필터 지우기
필터 지우기

Reading in an array using MATLAB Coder

조회 수: 2 (최근 30일)
ladybird
ladybird 2021년 3월 9일
댓글: Rena Berman 2021년 9월 20일
Hi, I used Matlab Coder to generate C code that performed matrix multiplication with a function called "matmult". The original code took in a 2D matrix of values in a txt file, let's call it "data", and multipled "data" by a rotation matrix. The C Code generated by MATLAB coder doesn't have any inputs, instead it creates its own n x n emxArray in the generated example main function, and passes that into the "matmult.c" function generated from the MATLAB code. I've searched the internet looking at resources on emx data types but I can't seem to figure out how I'm supposed to read in the needed data from the .txt file and then manipulate that so it can be used with the EMX data type. I looked at this https://www.mathworks.com/help/coder/ug/use-c-arrays-in-the-generated-function-interfaces.html#bsp2zlh documentation but it uses a function called emxArray_uint32_T which I don't have. There's a function in the C code called emxCreateWrapper_real_T , is that the same as emxArray_uint32_T & am I supposed to read in the "data" from the .txt file, store that it an array, and then wrap that newly created array in a emxCreateWrapper_real_T? Any help would be appreciated, thanks!

답변 (1개)

Walter Roberson
Walter Roberson 2021년 3월 9일
편집: Walter Roberson 2021년 9월 20일
Read the data into an array in any way convenient, and put the wrapper around it.
It is deliberate (I gather) that Coder supports very little in the way of file I/O. As best I can gather, the idea is that Coder is expected to be used a lot with embedded systems that do not have file i/o or which have file i/o based on platform specific calls instead of through calls provided in the C or C++ standard libraries.
IMHO I think there is plenty of room for Mathworks to have something like a Hardware Support Package that supports standard file i/o for Hosted Systems (in C terminology).
  댓글 수: 1
Ryan Livingston
Ryan Livingston 2021년 3월 14일
편집: Ryan Livingston 2021년 3월 14일
On the contrary, there's no deliberate choice to avoid file I/O in MATLAB Coder (source: I'm on the dev team). MATLAB Coder already supports several MATLAB low-level file I/O functions including fopen, fprintf, fread, fscanf, fclose:
that could help in this case. Those functions eventually generate calls to the standard C routines as one would expect with some extra logic to preserve MATLAB behaviors.
Another approach is to rewrite your entry-point function to take in the file name, and use those MATLAB functions to read in your data. Then there's no need to manually construct the emxArray input variables in your calling C code
function y = matmult(filename)
f = fopen(filename);
data = fscanf(f,'%g');
fclose(f);
% Your rotation matrix goes here
rotmat = eye(3);
data = reshape(data,size(rotmat));
y = rotmat * data;
end
% Write some test data and do codegen
f = fopen('mat.txt','w');
fprintf(f,'%g ',magic(3));
fclose(f);
codegen matmult -args 'mat.txt' -report
matmult_mex('mat.txt')
ans =
8 1 6
3 5 7
4 9 2
In the generated code, you'll declare an emxArray_real_T* to pass for y which the generated code will fill in with the results.

댓글을 달려면 로그인하십시오.

태그

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by