Run simulink model in for loop
64 次查看(过去 30 天)
显示 更早的评论
Hi,
I would like to run a simulink model and save results as .mat files in a for loop. I do this with for loop and working fine. Now, I would like to use parfor function. But I could not figure it out how it can be done. I am describing a simple model to explain my problem.
Matlab verson is : R2020a 64 bit in Windows pc, I have license for parallel computing tool box also.
-Simulink model consist of a variable voltage source connected across a resistor.
-Resistance of the resistor (R) changes in each for loop.
-Variable voltage source block creates sinusoidal voltage. The frequency of the voltage changes in each loop.
-Resistance and input to variable voltage source are defined in matlab work space. Simulink reads these parameters during simulation. In every occurance of the for loop these parameters vary.
-During simulation a scope records voltage across the resistor. After simulation it logs data to workspace as an array named 'volt'.
-This array volt is saved in the current directory as a .mat file with different file names.
With for loop the simulation goes perfectly fine. Code for for loop is given below.
clc
clear all
close all
for k=1:1:10;
f=k; %frequency of sine wave
R=50*k; %value of resistor
dt=1e-4; %time step
t=0:dt:10; %time vector
voltage=4e-4*sin(2*pi*f*t); %sinusoidal voltage
wave = struct(); %input to simulink wave block
wave.time = t';
wave.signals.values = [voltage'];
wave.signals.dimensions =1;
sim('Test.slx');
foldern=(['f',int2str(k)]);
save(foldern,'volt'); %Output from simscape scope 'volt' is saved as mat file with different names
Simulink.sdi.clear;
end
For using parfor, I tried different ways mentioned in the website. But I could not simulate. One of the try is given below. How can I modify my above code with for loop for using parfor?
clc
clear all
close all
% model = 'Test.slx';
myDictionaryObj = Simulink.data.dictionary.create('testdd2.sldd');
dd = 'testdd2.sldd';
spmd
Simulink.data.dictionary.setupWorkerCache
end
parfor k=1:1:10;
dictObj = Simulink.data.dictionary.open(dd);
sectObj = getSection(dictObj,'Design Data');
entryObj = getEntry(sectObj,'MODE');
temp = getValue(entryObj);
temp.Value = CtrlValues(index);
setValue(entryObj,temp);
%
f=k; %frequency of sine wave
R=50*k; %value of resistor
dt=1e-4; %time step
t=0:dt:10; %time vector
voltage=4e-4*sin(2*pi*f*t); %sinusoidal voltage
wave = struct(); %input to simulink wave block
wave.time = t';
wave.signals.values = [voltage'];
wave.signals.dimensions =1;
[volt]=runsimulation;
foldern=(['f',int2str(k)]);
save(foldern,'volt'); %Output from simscape scope 'volt' is saved as mat file with different names
discardChanges(dictObj);
close(dictObj);
end
spmd
bdclose(model)
Simulink.data.dictionary.cleanupWorkerCache
end
Please help me with this.
I am attaching .m file and .slx file of simulink model.
2 个评论
Raymond Norris
2020-9-17
编辑:Raymond Norris
2020-9-17
Hi Sreejith,
Could you provide more info about how the model could not simulate? Are you getting errors, warnings, incorrect results, etc.?
I'm not a Simulink user and can't provide an answer, but if you haven't already, you might consider looking at parsim
Raymond
回答(1 个)
Joel Van Sickel
2020-9-17
Hello Sreejith,
please use the parsim function for this type of application. If you want to use parfor, there are ways to do it, but they are more complicated.
If you want to use parfor, here is some example code. This code uses the controls toolbox to linearize a model: You will need to configure the pool and multiple directories for each one to run in.
Regards,
Joel
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Logging 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!