Clear Filters
Clear Filters

Passing variable from Simulink to function, which will be called in MATLAB Function Block

1 view (last 30 days)
I want to solve ODEs in Simulink by using MATLAB Function Block, but i couldn't write all code directly in the MATLAB FunctionBlock, cause 'ode23s' not supported for code generation. So i try to use coder.extrinsic and to call a function from another m-file. Below is a simple example.
But i don't know how to pass the mu value from Simulink to the standard function. How can i realize it or should i just write functions in Block and m-file in different ways?
My Simulink Mode:
and the MATLAB Function Block:
1. function y = func(mu)
3. coder.extrinsic('standard');
5. y = 0;
6. y = standard();
7. end
the standard function (that from other m-file) should be called in MATLAB Function Block:
1. function y = standard(mu)
3. odefcn = @(t,y) [y(2); mu*(1-y(2)^2)*y(2)-y(1)];
4. [t,Y] = ode45(odefcn, [0 20], [2; 0]);
6. y = Y(:,1);
7. end
But i always get the error:
An error occurred while running the simulation and the simulation was terminated
Caused by:
I have also already tried assignin( to save the value in workspace), and save (in a mat) in Block, also not supported for code generation. Thank you guys in advance:)

Answers (1)

Jyotsna Talluri
Jyotsna Talluri on 24 Feb 2020
In your code ,you are not passing the input to standard function .Pass the input variable to the standard function
function y = func(mu)
y = 0;
y = standard(mu);
The output of the MATLAB function block is default set to 1*1 size unless we enable the Variable size check box in the MATLAB Function block editor ,In the Edit data of the MATLAB Function block editor ,Specify the output signal size to be [309 1] as the standard function outputs a signal y of that size . Use the To Workspace block with Save format ‘ set to 'Structure' and 'Save 2-D signals as' set to ‘3-D array(concatenate along third dimension)’ and Sample time same as the 'Stop Time' of the model .
Refer to the below link for more information

Community Treasure Hunt

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

Start Hunting!