Embed an external m file in a simulink m function
16 views (last 30 days)
Show older comments
I , I have some long calculation codes written inside my simulink m function and a longif-elseif-else loop that repeatedly uses those same codes, the end result being my program has become cumbersome. In a normal m file, one can use nested function, and simply call the function. That is not possible in embedded matlab codes. I am trying to use coder.extrinsic keyword and call an external m file ( which has the repetitive files) while running my simulink model,unfortunately it gives an error message. Both my test model and my actual model gives an error, mostly that one input is not correct. Here is a sample of the test code in my test mdl file.
if true
function [y1,y2] = fcn(u1,u2,u3)
%#codegen
coder.extrinsic('-sync:on','test');
x1 = u1*u2;
x2 = u1*u2*u3;
x3 = u2*u3;
y1 = test(x1,x2);
y2 = test(x2,x3);
end
The test function looks as follows
if true
function out = test(a,b)
out = a+b;
end
end
The picture of error message is also attached. I really do not understand what is going wrong and would appreciate any help. I do not have license for Simulink coder so using a S function is not possible.
0 Comments
Accepted Answer
Ryan Livingston
on 31 Dec 2015
First, no need to have the if true ... end wrapping the whole file. Just omit that.
Next, it is possible to use sub-functions (not nested functions) in the MATLAB Function Block and you should be able to call general MATLAB functions in M files which are on the MATLAB path. The function test given could be used either as a sub-function in the MATLAB function block or as a function in its own MATLAB M file.
As an example, given the file, test.m, containing:
function out = test(a,b)
%#codegen
out = a+b;
just use the following for you function block:
function [y1,y2] = fcn(u1,u2,u3)
%#codegen
x1 = u1*u2;
x2 = u1*u2*u3;
x3 = u2*u3;
y1 = test(x1,x2);
y2 = test(x2,x3);
That should just work. coder.extrinsic is used to allow calling functions which contain constructs which are not supported for code generation during simulation. Since your proposed test function contains constructs which support code generation, just call it like any normal MATLAB function.
More Answers (0)
See Also
Categories
Find more on Deployment, Integration, and Supported Hardware in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!