MATLAB: How to extract all the logged variables from the model using the single-output format of the SIM function in Simulink 7.6 (R2010b)

simulink

I am running a script from MATLAB that simulates a Simulink model using the SIM command. In Simulink, I am logging signals using Scope blocks and To Workspace blocks.
In releases previous to R2009b, the variables logged in the model showed in the MATLAB base workspace after the completion of SIM. Since R2009b, the SIM command returns a single output containing all the logged signals.
What is the best way to extract all the logged signals so that I can post-process the logged data using the same script as in releases older than MATLAB R2009b?

Best Answer

The following code extracts all the logged variables from the model using the SIM function ans assign them to individual variables in the MATLAB workspace:
clear
mdl = 'mymodel';
open_system(mdl)
t_stop = 1.5;
% Passing parameters to SIM using a structure
paramStruct.StopTime = 't_stop';
paramStruct.ReturnWorkspaceOutputs = 'on';
% Simulate
simOut = sim(mdl,paramStruct);
%%Extract the logged signals
% Obtain list of logged parameters
paramList = simOut.who;
% Loop through the list and assign in base workspace
for i= 1:length(paramList)
assignin('base',paramList{i},simOut.get(paramList{i}));
end