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

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

 채택된 답변

Fangjun Jiang
Fangjun Jiang 2020년 3월 17일
편집: Fangjun Jiang 2020년 3월 17일
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

댓글 수: 3

Thank you very much Fangjun,
I encounter the following errors.
The following error occurred converting from timeseries to double:
Conversion to double from timeseries is not possible.
Error in pressureCombinations (line 13)
SavedTheta(i,j)=thetaw;
When I convert the format in to array, then the following error is again encountered.
Unable to perform assignment because the size of the left side is 1-by-1 and the size of the right side is
401-by-1.
Error in pressureCombinations (line 13)
SavedTheta(i,j)=thetaw;
So theta is a timeseries or array, then replace with this
SavedTheta=cell(length(P1),length(P2));
SavedTheta{i,j}=theta;
Hi Fangjun,
I want to read those values at the end. What's the way of reading savedTheta 2D array.
Btw, this array consisit thousands of data as I increase the intervals of P1 and P2.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Simulink에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by