MATLAB Answers

Save and plot the Simout from simulink for every iteration of For loop applied in MATLAB

4 views (last 30 days)
Mayur Madhukar Gawle
Mayur Madhukar Gawle on 30 Jun 2021
Answered: Pavan Guntha on 30 Aug 2021
I Wish to change the parameter of Block T1 of Simulink and then i need to plot the graph and compare these 5 plots.
Hence i have for loop in MATLAB and controlled the paramters from MATLAB script using set_param.
Please refer the profgram below.
The problem i m not getting the ouput for 5 different plots and i end up getting only the last value of the iteration. i.e
i get the output values for only parameter with block T1 = '1'.
how can i save the output from simout at each iteration and plot the graphs.
Please help. Thanks in Advance.
I m writing my program below for reference
open_system('Belgium');
for i = 1:5
if i==1
set_param('Belgium/T1','Value','0');
elseif i==2
set_param('Belgium/T1','Value','0.1');
elseif i==3
set_param('Belgium/T1','Value','0.2');
elseif i==4
set_param('Belgium/T1','Value','0.5');
else
set_param('Belgium/T1','Value','1');
sim('Belgium');
open_system('Belgium/Scope3');
x = zeros(102,80);
y = zeros(102,80);
voltage = zeros(102,1);
time = zeros(102,1);
voltage = ans.simout.data;
time = ans.simout.time;
x(:,i)= voltage;
y(:,i)= time;
plot(y(:,i),x(:,i));
end

Answers (1)

Pavan Guntha
Pavan Guntha on 30 Aug 2021
Hello Mayur,
For storing output of simout at each iteration, you could create a 2D array and use 'i' as index to write data into it as illustrated below:
voltage(i,:) = ans.simout.data;
time(i,:) = ans.simout.time;
You could also use 'hold' command to plot multiple graphs onto a single figure for comparison.
Hope this helps!

Community Treasure Hunt

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

Start Hunting!