1

In my code i'm changing my array (int*) and then I want to compare it into the matlab results.

since my array is big 1200 X 1000 element. this takes forever to load it into matlab

i'm trying to copy the printed output file into matlab command line...

for (int i = 0; i < _roiY1; i++)
{
    for (int j = 0; j < newWidth; j++)
    {
        channel_gr[i*newWidth + j] = clipLevel;
    }
}

ofstream myfile;
myfile.open("C:\\Users\\gdarmon\\Desktop\\OpenCVcliptop.txt");
for (int i = 0; i <  newHeight ; i++)
{
    for (int j = 0; j < newWidth; j++)
    {
        myfile << channel_gr[i * newWidth + j] << ", ";
    }
    myfile<<";" <<endl;
}

is there a faster way to create a readable matrix data from c++? into matlab?

2 Answers 2

2

The simplest answer is that it's much quicker to transfer the data in binary form, rather than - as suggested in the question - rendering to text and having Matlab parse it back to binary. You can achieve this by using fwrite() at the C/C++ end, and fread() at the Matlab end.

int* my_data = ...;
int my_data_count = ...;

FILE* fid = fopen('my_data_file', 'wb');
fwrite((void*)my_data, sizeof(int), my_data_count, fid);
fclose(fid);

In Matlab:

fid = fopen('my_data_file', 'r');
my_data = fread(fid, inf, '*int32');
fclose(fid);

It's maybe worth noting that you can call C/C++ functions from within Matlab, so depending on what you are doing that may be an easier architecture (look up "mex files").

Sign up to request clarification or add additional context in comments.

Comments

2

Don't write the output as text.

Write your matrix into your output file the way Matlab likes to read: big array of binary.

ofstream myfile;
myfile.open("C:\\Users\\gdarmon\\Desktop\\OpenCVcliptop.txt", ofstream::app::binary);
myfile.write((char*) channel_gr, newHeight*newWidth*sizeof(channel_gr[0]));

You may want to play some games on output to get the array ordered column-row rather than row-column because of the way matlab likes to see data. I remember orders of magnitude improvements in performance when writing mex file plug-ins for file readers, but it's been a while since I've done it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.