Converting .m to C code using Matlab Coder

1 view (last 30 days)
Hello,
I am trying to convert MATLAB script to C code using Matlab coder.
%Source file
10 load data.mat; % this mat file has data of the structure struct_1
11 for k=1:5
12 struct_2(k).struct_1=struct_1;
13 struct_2(k).Out=interp2(struct_2(k).struct_1.in1,struct_2(k).struct_1.in2,struct_2(k).struct_1.in3,struct_2(k).P,struct_2(k).Q,'makima');
14
15 end
I received data.mat from other source and asked me to convert it to C. When I try to convert it to C, I got error variable struct_1,struct_2 must be initialised before subscripting. So i went to source code, run it and in workspace i found fields of both struct's and given the same dimensions. Now I initialised with fields of struct_1
%codegen
struct_1 =struct('in1',zeros(5,10),...
'in2', zeros(5,10),...
'in3', zeros(5,10),...
...%there are some more fields
...
);
struct_2 = struct( 'struct_1',struct_1,...
'Out', zeros(100,3),...
'P', Zeros(100,3),...
'Q'zeros(100,3),...
... ...%there are some more fields
);
Now i try to generate C code, this time error in line 13 size Mismatch, to resolve I used coder.varsize, and then it is showing Interpolation method must be one of the following: 'cubic', 'linear', 'nearest', or 'spline' and function call failed. I used repmat for struct_2 then this error is gone. Finally I am in code generation step now it is showing
Error using unmeshgrid (line 28)
Grid values must be strictly increasing.
Error in interp2 (line 70)
unmeshgrid(X,2),unmeshgrid(Y,1));
Error in line 13
I searched in matlab documentation, mathwork answers, stackoverflow and try to reslove errors but it is getting complicated for each step.
I think initialising data i.e(zeros(5,10) from output is wrong), it would be nice if you tell me how to proceed. I am trying this from past 5 days, still im not yet suceed.
if there is something wrong in my question or if anything is unclear feel free to ask.
Thank You.

Answers (1)

Steven Lord
Steven Lord on 2 Apr 2021
Certain functions have additional limitations in a code generation context using MATLAB Coder. See the Extended Capabilities section at the end of the function's documentation page to see which extended capabilities it supports and whether or not there are any limitations.
For the interp2 function there is a C/C++ Code Generation item in the Extended Capabilities section that lists a few limitations. The documentation page for the load function also has a C/C++ Code Generation item with some usage notes and limitations.
  1 Comment
Naga Manoj Kumar Lakkoju
Naga Manoj Kumar Lakkoju on 2 Apr 2021
Edited: Naga Manoj Kumar Lakkoju on 2 Apr 2021
Thanks for your reply,
I have gone through documentation page and changed makima to spline. It is suggested to apply mesh grid to Xq, Yq( last two parameters of interp2 function). I applied mesh grid to struct_2(k).P,struct_2(k).Q and compared my results with original file, output is not the same. Moreover i am getting same error in coder i.e
Error using unmeshgrid (line 28)
Grid values must be strictly increasing.
Error in interp2 (line 46)
 unmeshgrid(X,2),unmeshgrid(Y,1));
Error in fun (line 13)
 struct_2(k).Out=interp2(struct_2(k).struct_1.in1,struct_2(k).struct_1.in2,struct_2(k).struct_1.in3,struct_2(k).P,struct_2(k).Q,'spline'); 
I tried using linear, cubic etc. but no luck.

Sign in to comment.

Categories

Find more on Descriptive Statistics in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!