MATLAB: Programatically changing block parameter during simulation.

simulink

I am automatically changing the amplitude value of my sin wave block in my model. But the problem is that my program runs the simulation multiple times and do not change the values of amplitude during one simulation. this is the code:
amplitude_vec=(0.5:0.2:2.3);
for i=1:10
amplitude=amplitude_vec(i);
sim('Amplifier.mdl');
end;
How do i write a program that will change the values of amplitude of my sin wave block in one simulation run ?
Thanks for your help,
Janez

Best Answer

amplitude_vec=(0.5:0.2:2.3);
for k=1:10
amplitude=amplitude_vec(k);
set_param('Amplifier/Sine Wave','Amplitude',num2str(amplitude))
%check if "Sine Wave" is the name of your sine block, if not, replace it
sim('Amplifier');
end;
or you can use (you don't need to pause simulation)
  1. the below code allows to create a signal with multiple amplitude saved in "file.mat"
  2. you set sine amplitude to 1
amplitude_vec=(0.5:0.2:2.3);
t0=0;tf=5;te=0.1;
tf1=tf;t=[];amp=[];
for k=1:length(amplitude_vec)
t1=t0:te:tf;
t=[t t1 ];
amp=[ amp amplitude_vec(k)*ones(1,length(t1))];
t0=tf+te;tf=t0+tf1
end
close;plot(t,amp);
ty=[t;amp];
save file ty