Simulink Matlab Function Block

3 次查看(过去 30 天)
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