Simulink Matlab Function Block

3 Ansichten (letzte 30 Tage)
Abdel Gafoor Haddad
Abdel Gafoor Haddad am 9 Nov. 2019
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

Antworten (0)

Kategorien

Mehr zu Programmatic Model Editing finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by