Simulink: Can't change model workspace variables using setVariable

I'm running a Simulink model programmatically using parsim. I pass input data into a series of SimulationInput objects using the setVariable function, but when I run my model using parsim the model workspace isn't using the values I passed in. What gives?
for index = 1 : length(inputs)
simIn = Simulink.SimulationInput('MySimulation');
simIn = setVariable(simIn, "RefractTimeConstant",simulationParameters.RefractTimeConstant(index),"Workspace",'MySimulation');
simulationInputConfigs(end+1) = simIn;
end
.
.
.
simOuts = parsim(simulationInputConfigs, 'ShowSimulationManager', 'on');

4 个评论

At the risk of asking the obvious: Any chance that the parameter in the model isn't called "RefractTimeConstant"? Maybe it's different, like "RefractionTimeConstant", or maybe it was entered into the model with a typo?
Does the code work as expected, using sim instead of parsim?
BTW, nice user name.
Thanks for responding! Like your username too :D
I've triple checked all the parameter names, they definitely match. I tried running the model using 'sim' instead of 'parsim' with the same results. I can see my variables stored in the SimulationInput objects, but for some reason they aren't used when running the model.
Did you find a solution to this? I have the exact same problem.. I'm trying to use the variable in a matlab-script in my simulinkmodel. Maybe that is the problem?
I'm not the OP, just have the same username.
I ran this code in 2022a in the Live Editor and it worked fine. Note that I'm using sim, not parsim, though the OP said he had the same problem with sim and parsim. Also, I wan't sure how the variable simulationInputConfigs was initialized, so I didn't use it. The model mysim is just a Constant 1, sent through a Gain, where the Gain parameter is theGain (I hope that's clear). Also, in mysim I do not have the variable theGain defined in the model workspace. The output of the Gain block goes To Workspace. The end result is that y had the values 1:5, as expected.
Gains = 1:5;
for index = 1 : 5
simIn(index) = Simulink.SimulationInput('mysim');
simIn(index) = setVariable(simIn(index), "theGain",Gains(index),"Workspace",'mysim');
% simulationInputConfigs(end+1) = simIn;
end
% check output for the expected result.
simOuts = sim(simIn, 'ShowSimulationManager', 'on');
for ii = 1:5
y(ii) = simOuts(ii).y.Data(end);
end

请先登录,再进行评论。

回答(0 个)

类别

帮助中心File Exchange 中查找有关 Simulink Functions 的更多信息

产品

版本

R2022b

提问:

2022-10-31

评论:

2022-12-29

Community Treasure Hunt

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

Start Hunting!

Translated by