Muk
Muk

Reputation: 11

How to save a c++ readable .mat file

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 this to me.

Upvotes: 0

Views: 6218

Answers (4)

Pursuit
Pursuit

Reputation: 12345

It seems that you have a lot of options here, depending on your exact needs, time, and skill level (in both Matlab and C++). The obvious ones are:

ASCII files

You can generate ASCII files in Matlab either using the save(filename, variablename, '-ascii') syntax, or you can create a more custom format using c-style fprintf commands. Then, within a C or C++ program the files are read using an fscanf.

This is often easiest, and good enough in many cases. The fact that a human can read the files using notepad++, emacs, etc. is a nice sanity check, (although this is often overrated).

There are two big downsides. First, the files are very large (an 8 byte double number requires about 19 bytes to store in ASCII). Second, you have to be very careful to minimize the inevitable loss of precision.

Bytes-on-a-disk

For a simple array of numbers (for example, a 32-by-32 array of doubles) you can simply use the fwrite Matlab function to write the array to a disk. Then within C/C++ use the parallel fread function.

This has no loss of precision, is pretty fast, and relatively small size on disk.

The downside with this approach is that complex Matlab structures cannot necessarily be saved.

Mathworks provided C library

Since this is a pretty common problem, the Mathworks has actually solved this by a direct C implementation of the functions needed to read/write to *.mat files. I have not used this particular library, but generally the libraries they provide are pretty easy to integrate. Some documentation can be found starting here: http://www.mathworks.com/help/matlab/read-and-write-matlab-mat-files-in-c-c-and-fortran.html

This should be a pretty robust solution, and relatively insensitive to changes, since it is part of the mainstream, supported Matlab toolset.

HDF5 based *.mat file

With recent versions of Matlab, you can use the notation save(filename, variablename, '-v7.3'); to force Matlab to save the file in an HDF5 based format. Then you can use tools from the HDF5 group to handle the file. Note a decent, java-based GUI viewer (http://www.hdfgroup.org/hdf-java-html/hdfview/index.html#download_hdfview) and libraries for C, C++ and Fortran.

This is a non-fragile method to store binary data. It is also a bit of work to get the libraries working in your code.

One downside is that the Mathworks may change the details of how they map Matlab data types into the HDF5 file. If you really want to be robust, you may want to try ...

Custom HDF5 file

Instead of just taking whatever format the Mathworks decides to use, it's not that hard create a HDF5 file directly and push data into it from Matlab. This lets you control things like compression, chunk sizing, dataset hierarchy and names. It also insulates you from any future changes in the default *.mat file format. See the h5write command in Matlab.

It is still a bit of effort to get running from the C/C++ end, so I would only go down this path if your project warranted it.

Upvotes: 4

johnish
johnish

Reputation: 390

Adding to what has already been mentioned you can save your data from MATLAB using -ascii.

save x.mat x

Becomes:

save x.txt x -ascii    

Upvotes: 0

Dan
Dan

Reputation: 45752

You can also use matlab's dlmwrite to write to a delimited asci file which will be easy to read in C (and human readable too) although it may not be as compressed if that is core to the issue

Upvotes: 0

TJ1
TJ1

Reputation: 8498

.mat is special format for the MATLAB itself. What you can do is to load your .mat file in the MATLAB workspace:

load file.mat

Then use fopen and fprintf to write the data to file.txt and then you can read the content of that file in C.

Upvotes: 1

Related Questions