From: Jean V.G on
I'm writing some software to strip image data from a camera link using
C++ but my analysis will be done in Matlab. In my C++ code, the image
data is stored in an array of type float and size 76800 (i.e. image
size = 320 × 240). The array is defined as 'float pBuffer[76800];'

Currently I'm saving this array into a text file, then import the txt
file in Matlab, then use function 'reshape' to change to 320 × 240,
then save the workspace data to a .mat file. My questions are:

1) What is the best way to save the data in C++ other than txt file as
I find it takes a lot of space, 669 KB for text file compared to 80 KB
for final .mat file? I'm saving the data as below:

void saveArray(float* array, int length)
{
ofstream output("output.txt");
for(int i=0;i<length;i++)
{
output<<array[i]<<endl;
}
}

2) Can I 'reshape' to get a 2-D array and save as .mat file in C++
itself? Is there a library for that?

I realize that there are more questions about C++ than Matlab but they
are all linked and I hope someone here can help me on this.

Thanks.
From: Ashish Uthama on
On Fri, 26 Mar 2010 11:17:57 -0400, Jean V.G <viv3kg(a)gmail.com> wrote:

> I'm writing some software to strip image data from a camera link using
> C++ but my analysis will be done in Matlab. In my C++ code, the image
> data is stored in an array of type float and size 76800 (i.e. image
> size = 320 � 240). The array is defined as 'float pBuffer[76800];'
>
> Currently I'm saving this array into a text file, then import the txt
> file in Matlab, then use function 'reshape' to change to 320 � 240,
> then save the workspace data to a .mat file. My questions are:
>
> 1) What is the best way to save the data in C++ other than txt file as
> I find it takes a lot of space, 669 KB for text file compared to 80 KB
> for final .mat file? I'm saving the data as below:
>
> void saveArray(float* array, int length)
> {
> ofstream output("output.txt");
> for(int i=0;i<length;i++)
> {
> output<<array[i]<<endl;
> }
> }
>
> 2) Can I 'reshape' to get a 2-D array and save as .mat file in C++
> itself? Is there a library for that?
>

http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_external/f39876.html#f13830
Follow links to specific functions (which also have code examples)

You might run into transposing issues (C is row major while MATLAB is
column major).


> I realize that there are more questions about C++ than Matlab but they
> are all linked and I hope someone here can help me on this.
>
> Thanks.
From: Máday Péter on

Hello

If you write your array of floats into a binary file e.g. array.dat,
then there should be no problem getting the array data into MATLAB by
fread:

C++

void saveArray(float* array, int length)
{
ofstream output("output.txt", std::ios::binary);
for(int i=0;i<length;i++)
{
output<<array[i];
}

output.close();
}

MATLAB

fp = fopen('array.dat', 'rb');
arrayData = fread(fp, 'float32');
fclose (fp);

from arrayData you can reconstruct your image by using reshape() and a
transpose due to the column / row major issue.

imageData = reshape(arrayData, imageWidth, imageHeight)'; % mind the
transpose

Peter