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

조회 수: 6 (최근 30일)
Marcus
Marcus 2013년 12월 9일
댓글: Kaustubha Govind 2013년 12월 18일
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

답변 (1개)

Edric Ellis
Edric Ellis 2013년 12월 10일
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
Marcus
Marcus 2013년 12월 11일
편집: Marcus 2013년 12월 11일
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 2013년 12월 18일
Is the 'frequency' value for the powergui block or any source block in your model set to a workspace variable?

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

카테고리

Help CenterFile Exchange에서 Simulation and Analysis에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by