I want to run a model for different input parameters, using an arraySimulink.SimulaitonInput Objects.
In the StopFcn of the Model I want to read some of the model parameters and variables set by the simulink.SimulationInput objects.
To access the parameters I can call
get_param(gcs,'Parameter')
to get the value of the parameter as set by the Simulink.SimulationInput object.
However to access variables of the model I would need to do:
mdlWks = get_param(gcs,'ModelWorkspace');
Variable = getVariable(mdlWks,'Variable');
This however only gives me the default version of the value for Variable. I waat the current set value of Variable though. Is there an alternative get_var function or something that can do this?

 채택된 답변

Paul
Paul 2023년 1월 16일

0 개 추천

Instead of using the StopFcn, maybe you can use setPostSimFcn functionality

댓글 수: 7

Thanks for your answer. From I read the documentation, the post sim function only has access to the simulation output object. From where I think I cant access the variables.
The documentation, at least in 2022a, is ... less than clear. Anyway, here's an example.
I have a simulaion 'untitled' that uses one variable, "theConstant." Illusturate functionality to pass the simIn.Variables to the output of the sim command.
Create simIn
simIn = Simulink.SimulationInput('untitled');
Set theConstant in simIn. Use the model workspace
simIn = setVariable(simIn,'theConstant',5,'Workspace','untitled');
simIn.Variables
ans =
Variable with properties:
Name: 'theConstant'
Value: 5
Workspace: 'untitled'
Description: ""
Set the post sim function using an anonymous function. Use simIn.Variables as the input to simpostfunc
simIn = setPostSimFcn(simIn,@(simout) simpostfunc(simIn.Variables));
Run the simulation and verify that simIn variables are copied to the sim output via simpostfunc
y = sim(simIn);
y
y =
Simulink.SimulationOutput:
simvars: [1x1 Simulink.Simulation.Variable]
SimulationMetadata: [1x1 Simulink.SimulationMetadata]
ErrorMessage: [0x0 char]
y.simvars
ans =
Variable with properties:
Name: 'theConstant'
Value: 5
Workspace: 'untitled'
Description: ""
function newout = simpostfunc(simvariables)
% simple function to echo the simIn variables to the the simulation output
newout.simvars = simvariables;
end
I see what you are doing. But that does not solve the problem. You hand the data over directly at the beginning of the simulation. My case now is, i have an array of simIn objects with variable 'a' running 1 through 5, and i want to have the corresponding value of a available at the end of every of the five simulations.Your approach would make the array of sim in objects available in the PostSimFcn, without knowldege of which of them I need.
Each element of the simIn array has its own PostSimFcn. Here is code that binds a unique PostSimFcn to each element, storing the the variable used for each run:
for ii = 1:5
simIn(ii) = Simulink.SimulationInput('untitled');
simIn(ii) = setVariable(simIn(ii),'theConstant',ii,'Workspace','untitled');
simIn(ii) = setPostSimFcn(simIn(ii),@(simout) simpostfunc(simIn(ii).Variables));
end
y = sim(simIn);
y
y.simvars
y =
1x5 Simulink.SimulationOutput array
ans =
Variable with properties:
Name: 'theConstant'
Value: 1
Workspace: 'untitled'
Description: ""
ans =
Variable with properties:
Name: 'theConstant'
Value: 2
Workspace: 'untitled'
Description: ""
ans =
Variable with properties:
Name: 'theConstant'
Value: 3
Workspace: 'untitled'
Description: ""
ans =
Variable with properties:
Name: 'theConstant'
Value: 4
Workspace: 'untitled'
Description: ""
ans =
Variable with properties:
Name: 'theConstant'
Value: 5
Workspace: 'untitled'
Description: ""
As expected, y is a 5-element array of Simulink.SimulationOutput objects, and each element of y contains the value of theConstant that was used in the simulation that generated that element of y.
Thanks. This would be a solution to the problem. But maybe the problem then was, that my requiremnt would have been to not build any loop or additional structrue around this. Instead of your approach I can just run all simulations and then pass the array of inputs and the array of outputs to a function that handles both.
Paul
Paul 2023년 1월 19일
Well, you could just as easily do that. But the original qeustion started with using using the StopFcn, so I thought you wanted to use automatic callback functionality. Either way, sounds like you have a path forward.
I just realized that the code I posted will overwrite output data in output of sim. The corrections would be pass the sim output into the post sim function, modify it, and return it.
simIn(ii) = setPostSimFcn(simIn(ii),@(simout) simpostfunc(out,simIn(ii).Variables));
function out = simpostfunc(out,simvariables)
% simple function to echo the simIn variables to the the simulation output
out.simvars = simvariables;
end

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Programmatic Model Editing에 대해 자세히 알아보기

제품

릴리스

R2022a

질문:

2023년 1월 16일

댓글:

2023년 1월 22일

Community Treasure Hunt

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

Start Hunting!

Translated by