MATLAB: Running simulink model in a for loop and separately storing variable data in each loop

data acquisitionMATLABsimulink

Hi ,
I want to run a Simulink model in a .m file for loop and keep values of each iteration without changing in the next loop cycle. Is there any possibility for you to help me out with this?
In the following code, 'TestBlock' runs 2s and, sends a variable; 'theta' to the workspace. I want to store that theta value and next consequent theta values till the loop will be over.
P1=1:1:5;
P2=2:1:5;
for i=1:(length(P1))
for j=1:(length(P2))
P1_in=P1(i);
P2_in=P2(j);
sim('TestBlock');
end
end

Best Answer

If theta is a scalar,
P1=1:1:5;
P2=2:1:5;
SavedTheta=zeros(length(P1),length(P2));
for i=1:(length(P1))
for j=1:(length(P2))
P1_in=P1(i);
P2_in=P2(j);
sim('TestBlock');
SavedTheta(i,j)=theta;
end
end