How can I use a parfor loop in matlab for a simulink simulation using structs?

6 views (last 30 days)
Hello,
I have a simulink model with different outputs (to workspace - matrix) and one input x (factor) that I want to run in a parfor loop. The simulation takes hours so I wanted to use parallel computing, storing the results in structs (or what else would be possible?). I always get the message 'syntax error' - (its working as a normal for loop) who can show me the problem? thanks! marcus
tic
matlabpool open 8
factor= 1:10;
parfor counter= 1:10
x = factor(counter);
sim('M_Campusnetz_working_modell');
counter.(['Durchlauf_' num2str(counter)]).Voltage= simout_NVP_Voltage;
counter.(['Durchlauf_' num2str(counter)]).Current= simout_NVP_Current;
end
matlabpool close
toc

Answers (1)

Edric Ellis
Edric Ellis on 10 Dec 2013
There's lots of information about using Simulink inside PARFOR here. I think you need to use the single-output-argument form of the SIM command.
  2 Comments
Marcus
Marcus on 11 Dec 2013
Edited: Marcus on 11 Dec 2013
Thanks so far!
I use the powergui in my electrical system in simulink. Are there any problems using parfor in combination witz the powergui? My script is working as long as I the for loop under 4) is not a parfor loop - this results in the following error:
There is no voltage source, current source, or machine block with a frequency matching the Phasor simulation frequency.
clear all close all clc
% 1) Load model and initialize the pool.
model = 'M_Campusnetz_working_modell';
matlabpool('open');
% 2) Vektor mit den Faktoren
Vektor_Faktoren = (1:1:3);
anzahl = length(Vektor_Faktoren);
simout(anzahl) = Simulink.SimulationOutput;
%simOut(2, anzahl) = Simulink.SimulationOutput;
% 3) Need to switch all workers to a separate tempdir in case
% any code is generated for instance for StateFlow, or any other
% file artifacts are created by the model.
parfor idx=1:8
% Setup tempdir and cd into it
addpath(pwd);
currDir{idx} = pwd;
addpath(currDir{idx});
tmpDir{idx} = tempname;
mkdir(tmpDir{idx});
cd(tmpDir{idx});
% Load the model on the worker
load_system(model);
end
% 4) Loop over the number of iterations and perform the
% computation for different parameter values.
parfor idx=1:anzahl
load_system(model);
Faktor = Vektor_Faktoren(idx);
set_param([model '/SERF/Gain'], 'Gain', num2str(Faktor));
set_param([model '/SERF/Gain1'], 'Gain', num2str(Faktor));
simout(idx) = sim(model, 'SimulationMode', 'normal', ...
'SaveOutput','on','OutputSaveName','simout_NVP_Spannung',...
'SaveOutput','on','OutputSaveName','simout_NVP_Strom');
end
% 5) Switch all of the workers back to their original folder.
parfor idx=1:8
cd(currDir{idx});
rmdir(tmpDir{idx},'s');
rmpath(currDir{idx});
end
for idx = 1:anzahl
Ergebnisse.(['Durchlauf_' num2str(idx)]).Spannung = simout(1,idx).get('simout_NVP_Spannung');
Ergebnisse.(['Durchlauf_' num2str(idx)]).Strom = simout(1,idx).get('simout_NVP_Strom');
end
save_system(model);
close_system(model);
matlabpool('close');
Kaustubha Govind
Kaustubha Govind on 18 Dec 2013
Is the 'frequency' value for the powergui block or any source block in your model set to a workspace variable?

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!