Loading Files into Codegen Files
12 views (last 30 days)
Is there any good way to load data from files (hopefully MAT-files) into a Matlab function that will be going through the Matlab coder? I've been looking into using a bunch of coder.eval calls into the C MAT-file library, but it looks like this process is going to be exceedingly painful and I'm not entirely sure it will let me return arrays anyway, since ceval can only return Scalars...
Kaustubha Govind on 18 Apr 2012
I think using coder.ceval is the right way to do this. Also, coder.ceval can return arrays/matrices. You just need to pre-allocate the return value so that it knows that type to expect. I haven't tried this before though, so please post back if you run into specific issues.
coder.ceval does indeed return only scalar values as mentioned in the documentation. I was able to generate code for this operation using this MATLAB code:
function X = testReadMAT()
X = 0;
SS_Table_file = coder.opaque('MATFile *');
SS_Table_file = coder.ceval('matOpen','datatable.mat','r');
Xp = coder.opaque('mxArray *');
Xp = coder.ceval('matGetVariable',SS_Table_file,'X');
X = coder.ceval('mxGetPr',Xp);
>> codegen -c -config:exe testReadMAT.m
There is still work to be done to build the code against the MAT-file API however.
More Answers (3)
1 1 on 25 Nov 2013
Hi Kaustubha, can you please explain how i can use coder to return matrix, i tried to use your trick mentioned above without success. Thanks in advance. Avi
Ryan Livingston on 26 Nov 2013
The function coder.load is now supported which loads data from a MAT file at compile-time as constants:
The syntax is basically the same as LOAD in MATLAB.
Also, LOAD is now supported in MEX. Search for load here for more details: