MATLAB Answers

Greg
0

Loading Files into Codegen Files

Asked by Greg
on 18 Apr 2012
Latest activity Edited by Carlos Pereira on 9 May 2016
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?

  1 Comment

Dear friends,
I have a quite similar problem. Using MATLAB coder I want to convert a matlab function to mex file. Furthermore, on this function I called a matrix (2001x2 double) from a MAT file. I´m not getting a way to make it. With the "testReadMAT" I can´t call the matrix from .MAT in "X". I think you had the same problem and, if it´s possible, help me for fix it.
I´m looking forward for answers from any users!!!!

Sign in to comment.

Products

4 Answers

Answer by Kaustubha Govind on 18 Apr 2012
 Accepted 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.

  2 Comments

Greg
on 18 Apr 2012
I've tried something like this:
SS_Table_file = coder.opaque('MATFile *');
SS_Table_file = coder.ceval('matOpen','datatable.mat','r');
X = double([]);
X = coder.ceval('matGetVariable',SS_Table_file,'X');
When it compiles, that generates an error that says "C function calls always return scalar values but a non-scalar value is expected here." (And it doesn't help if I preallocate for the correct size of matrix).
Also tried the mxArray pointer to a double pointer on the C side by replacing that last line with:
Xp = coder.opaque('mxArray *');
Xp = coder.ceval('matGetVariable',SS_Table_file,X);
X = coder.ceval('mxGetPr',Xp);
But that also generates the same error. Is there some way that I'm not thinking of to convert a C double pointer into a Matlab array?
Greg: Sorry about my earlier answer! Yes, you are correct that coder.ceval only returns scalar values. It does indeed say so clearly in the documentation page for coder.ceval.
I was actually able to generate code using your second approach. I will edit my answer to provide the code.

Sign in to comment.


Answer by Greg
on 28 Apr 2012

I've tried running code identical to what you had there, and I can't get it to compile. (I think it does successfully codegen, but the generated code doesn't compile). I'm trying to do this through the Simulink "Matlab Function" block which uses the Coder. When it tries to compile the code, I get a lot of errors like "'MATFile' : undeclared identifier", which says to me that the "mat.h" library file isn't being included, but I can't find anywhere to include it. I've tried adding "C:\Program Files\MATLAB\R2012\bin\win32\mat.h" to the "Libraries" box (that being where "libmat.dll" lives) in the Simulation Target -> Custom Code GUI, but that doesn't seem to help. If i just try a coder.ceval('#include "mat.h"'), I get a whole lot of noise in the compilation about "syntax error : missing ';' before 'type'".

  1 Comment

The location of mat.h is $matlabroot/extern/include/mat.h, so you need to make sure that "C:\Program Files\MATLAB\R2012\extern\include\mat.h" exists and add it using a #include statement. That is, under "Header File" add #include "mat.h" and under "Include Directories", add the path.

Sign in to comment.


Answer by 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

  0 Comments

Sign in to comment.


Answer by 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:

  0 Comments

Sign in to comment.