MATLAB: Loading Files into Codegen Files

matlab coder

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…
Any ideas?

Best Answer

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.
EDIT:
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()
%#codegen
if ~isempty(coder.target)
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');
coder.ceval('matClose', SS_Table_file);
X = coder.ceval('mxGetPr',Xp);
end
end
>> codegen -c -config:exe testReadMAT.m
There is still work to be done to build the code against the MAT-file API however.