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

1 visualización (últimos 30 días)
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)
2.
3. coder.extrinsic('standard');
4.
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)
2.
3. odefcn = @(t,y) [y(2); mu*(1-y(2)^2)*y(2)-y(1)];
4. [t,Y] = ode45(odefcn, [0 20], [2; 0]);
5.
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:
mu
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:)

Respuestas (1)

Jyotsna Talluri
Jyotsna Talluri el 24 de Feb. de 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)
coder.extrinsic('standard');
y = 0;
y = standard(mu);
end
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!

Translated by