How to store 3D matrix in text file so that it can be imported to Matlab?

c++, io, matlab

Solution

I found a hack without using any extra libs. I just output every 2d matrix as outMat(:,:,matIndex) and incremented matIndex in a loop. And then I ran the .m in matlab as a script.

void printArrs(){
    int i;
//  B(:,:,1) = [1 2 3; 4 5 6];
//  B(:,:,2) = [7 8 9; 0 0 0];
    ofstream outFile;
    outFile.open ("forPlot.m", ios::out | ios::app);
    matIndex++;
    outFile << "outMat(:,:," << matIndex << ") = [";
    for(i=0;i<fftLen;i++){
        outFile << Mag[0][i] << " ";
        outFile << Mag[1][i] << " ";
        outFile << Mag[2][i] << ";" << endl;
    }
    outFile << "];" << endl;
    outFile.close();
}

Thanks everyone for your answers. For some answers I wasnt clear enough I guess, because they assumed I want to write 'from' Matlab and not 'to' Matlab while it was the other way.

Problem

I have a text file as an output of a c++ program. Its actually a 3x100x200 element matrix. 3x100 2d matrices over 200 timestamps. I want to store this such that I can load it in Matlab workspace and then visualize it in a 3d plot. I am not able to figure out the structure of the text file. As in where should I put a "[..]" and where ";" and where a " " or ",". Could someone please give an example so that I can print out in the file from the c++ code in a that manner

Original source

Related problems