converting a matlab array (MAT-file) to C++ array

I have a 2-D double-precision array in MATLAB that contains specific data. I want to open and use this array in my c++ program, in the first step I save it in a mat-file. I know that MATLAB has some c functions that provide reading mat-file in c++ (matdsgn , matOpen , ...), but I have no idea how to use these functions in a c++ program. Actually, I don't know how to use a C library in C++. Any help would be appreciated.

Answers (1)

It would be easier to save the data as binary file:
data = rand(8, 10);
fid = fopen('File.data', 'w');
if fid == - 1
error('Cannot open file for writing');
end
fwrite(fid, ndims(data), 'uint16');
fwrite(fid, size(data), 'uint64');
fwrite(fid, data, 'double');
fclose(fid);
The reading from C++ is equivalent: Read the number of dimensions as uint16, then the dimension itself as uint64 and then the actual data.

Asked:

on 28 Aug 2017

Answered:

Jan
on 28 Aug 2017

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!