How do I append data to a MAT file from C++?
2 views (last 30 days)
From a C++ program, I would like to continuously append data to a MAT file while the program runs. The program doesn't have to append to an existing file, but after creating the file at the beginning the program needs to append save data to the MAT file over long periods of time. mxCreateNumericMatrix appears to require the size of the variable to be known with no options to append. The program cannot keep all the data in memory and then write it upon completion.
James Tursa on 30 Jun 2015
Why does it have to be a mat file? The simplest solution is to simply write the values in binary as you go, and then when you are done close the file. Then in MATLAB use fopen and fread to read in the data (in pieces if necessary because of memory constraints).
More Answers (2)
Adam Hug on 30 Jun 2015
I think your best bet will be to use MATLAB Engine. This is an API that lets you run MATLAB commands using C/Fortran programs. See the following doc link for more information and examples:
Muthu Annamalai on 1 Jul 2015
Edited: Muthu Annamalai on 1 Jul 2015
The two alternatives are nicely laid out by Adam, and James.
- Adam's recommendation is to use the Mex API.
- James's recommendation is to use the load(), csvload() like commands from MATLAB
I think following James's approach maybe quicker, and all you need in the short run.