MATLAB Answers


How to save a c++ readable Mat file

Asked by Andrew
on 30 Jan 2013

I am running a DCT code in matlab and i would like to read the compressed file(.mat) into a c code. However, am not sure this is right. i have not yet finished my code but i would like to request for an explanation of how to create a c++ readable file from my mat file. Am kinda confused when it comes to .mat, .txt and then binary, float details of files. Someone please explain to me this.



No products are associated with this question.

1 Answer

Answer by James Tursa
on 30 Jan 2013
Edited by James Tursa
on 30 Jan 2013

Are you trying to read the compressed mat file into C++ without the aid of the MATLAB API library? If so, this is not at all trivial. It would be better to write the files out using fwrite, which would be straightforward to read into C++. If you are using the MATLAB API library then there are several functions available you can use listed here:



on 30 Jan 2013

i am using "save" to write these files but i realised that am carrying some coefficients in the same compressed file which makes it quite hard to get a real result in the c code. the c code is supposed to read in a 256*256 array of data but my DCT file actually has a 32*32 data array. below is the example of how i am saving my DCT data in matlab, where "I_runcode" is the compressed data and the rest are functions. could you please explain to me how i can save this data(I_runcode) to make it readable in c++.(Just in case, this link is where i have my code.

% Saving the Compressed Code to Disk.
save ('Compressed.txt','I_runcode','I_dim','N','M','n','m','Norm_Mat','I');

Again I have to ask you, will you be running the C++ code on a machine that has MATLAB and thus you have access to the MATLAB API library of functions?

on 31 Jan 2013

yes sir, i am running both programs(c++ and matlab) on the same machine.

Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!