Loading Files into Codegen Files
조회 수: 24 (최근 30일)
이전 댓글 표시
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
Carlos Pereira
2016년 5월 9일
편집: Carlos Pereira
2016년 5월 9일
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!!!!
채택된 답변
Kaustubha Govind
2012년 4월 18일
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
Kaustubha Govind
2012년 4월 19일
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.
추가 답변 (3개)
Greg
2012년 4월 28일
댓글 수: 1
Kaustubha Govind
2012년 4월 30일
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.
1 1
2013년 11월 25일
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
Ryan Livingston
2013년 11월 26일
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
참고 항목
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!