Use Simulink PreSimFcn to set metadata in a Simulink.SimulationOutput object
4 views (last 30 days)
Show older comments
Is it possible to store variables from a simulation's PreSimFcn in the Simulink.SimulationOutput object's metadata?
I am setting up my Simulink.SimulationInput objects as:
for casenumber = 1:no_of_cases_to_run
inputs = {'Arg1','Arg2','ArgN','casenumber'};
simIn(casenumber) = Simulink.SimulationInput(model);
for iArg = 1:length(BD29Inputs)
eval(['simIn(casenumber) = simIn(casenumber).setVariable(''',inputs{iArg},''',',inputs{iArg},')'])
end
% Use a temporary variable for the SimIn element to avoid runaway memory usage when using a preSimFcn with parsim
tmpSimIn = simIn(casenumber);
% Set PreSimFcn to setParSimBaseWorskpaceVariables, which runs the function required to be run in parallel
simIn(casenumber) = simIn(casenumber).setPreSimFcn(@(x) setParSimBaseWorkspaceVariables(tmpSimIn));
end
simOut = parsim(simIn);
My PreSimFcn is
function setParSimBaseWorkspaceVariables(simIn)
% Assign variables required by the IPTOS_BD29_Simulation function
% to the function's workspace
varList = {'Arg1','Arg2','ArgN','casenumber'};
nVarsAssigned = 0;
for iVar = 1:length(simIn.Variables)
if nVarsAssigned == length(varList)
break
elseif ~isempty(find(strcmp(simIn.Variables(iVar).Name,varList), 1))
eval(strcat(simIn.Variables(iVar).Name,'=simIn.Variables(iVar).Value;'));
nVarsAssigned = nVarsAssigned + 1;
end
end
% Run the function required to be run in parallel
funcOutput=func(Arg1,Arg2,ArgN);
% Save funcOut
save(fullfile(iptos_output_file_directory,['Results_',num2str(casenumber),'.mat']),'funcOutput')
% Need help here - how to store 'funcOutput' as metadata in simOut
setUserData(simOut,funcOutput)
end
0 Comments
Answers (1)
Paul
on 16 May 2023
Hi Afzal,
You can store data into the Simulink.SimulationOutput object via the SimPostFcn. Here is an example.
0 Comments
See Also
Categories
Find more on Run Multiple Simulations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!