Simulink Matlab Function Block

조회 수: 3 (최근 30일)
Abdel Gafoor Haddad
Abdel Gafoor Haddad 2019년 11월 9일
편집: Abdel Gafoor Haddad 2019년 11월 9일
Hello,
I am trying to simulate a model using parsim. I would like to simulate the model with multiple function handles.
Since I could not pass the function handles directly to any block, I tried to utilize a Matlab function block.
This is the code in Matlab command:
model = 'model_name';
load_system(model)
in(1) = Simulink.SimulationInput(model);
in(2) = Simulink.SimulationInput(model);
C{1}= 'u(1)+2'; C{2} = 'u(1)+5'; % The two expressions are placed in a cell array
in(1) = in(1).setVariable('qq',1);
in(2) = in(2).setVariable('qq',2);
out = parsim(in, 'ShowProgress', 'on', 'TransferBaseWorkspaceVariables', 'on')
Then in the Simulink model, I use the Matlab function block with:
function y = fcn(u,qq) % qq is taken from a constant block
coder.extrinsic('evalin','strcat')
y=0;
C = evalin('base','C{qq}');
C =strcat('@(u)',C);
C = str2func(C);
y = C(u);
This works without any error, but gives 2 identical outputs, although the elements of C are different. Specifically, the line
C = evalin('base','C{qq}');
is evaluating only the first element of the cell array C regardless of the value of qq. So both simulations in the parsim are performing u(1)+2.
Note: the model runs fine when setting q=1 or q=2 in the command window then running the Simulink model manually. The problem only appears when running the model from the command window.
Thank you

답변 (0개)

카테고리

Help CenterFile Exchange에서 Programmatic Model Editing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by