Saving for loop value after each iteration

조회 수: 4 (최근 30일)
Sugar Mummy
Sugar Mummy 2022년 1월 20일
댓글: Image Analyst 2022년 1월 20일
Hello everyone!
I am working on simulink MATLAB (2021b) where I am simulating a model. I am facing problem in saving the value of resistance, the code shown below give [ ] for the value of resistance.
Other two variables values are working fine for all 3 values of resistance. I am attaching my code
Any help would be appreciated! Thanks in advance
PS: The constant block picks all values of resistance at each iteration!
resistance = [0.72;0.983;0.221];
R = resistance;
for i =1:numel(R)
resistance=R(i)
simOut=sim('ModelName.slx','SimulationMode','normal')
a{i}=find(simOut,'Variable1')
b{i}=find(simOut,'Variable2')
c{i}=find(simOut,'resistance') %somewhere here
end

답변 (1개)

Image Analyst
Image Analyst 2022년 1월 20일
Since resistance is already an array, simply delete the line where you overwrite the array with the i'th value of it:
resistance = [0.72;0.983;0.221];
R = resistance;
for i =1:numel(R)
simOut=sim('ModelName.slx','SimulationMode','normal')
a{i}=find(simOut,'Variable1')
b{i}=find(simOut,'Variable2')
c{i}=find(simOut,'resistance') %somewhere here
end
Now R and resistance both have all the original values.
  댓글 수: 4
Sugar Mummy
Sugar Mummy 2022년 1월 20일
@Image Analyst Sir, I completely agree to your observation you have made for the error BUT this is the complete code I have pasted here.
The output I want seems like this for file 0.72
  • One column of Voltage (Variable1)
  • Second column of Current (Variable2)
  • Third column of Resistance (which is giving either [ ] or dimesional error)
and same output for the remaining two values.
The code is EXACTLY the same which is running fine for Variable1 and Variable2 but not giving a constant value of resistance for a time period (2000 sec)
Hope I have conveyed my point now. Sorry for the inconvinence Sir.
Image Analyst
Image Analyst 2022년 1월 20일
Let's see. Let's run it in MATLAB online:
resistance = [0.72;0.983;0.221];
R = resistance;
for i =1:numel(R)
resistance=R(i)
simOut=sim('ModelName.slx','SimulationMode','normal')
a{i}=find(simOut,'Variable1')
b{i}=find(simOut,'Variable2')
c{i}=find(simOut,'resistance') %somewhere here
end
resistance = 0.7200
No system or file called 'ModelName.slx' found.
Is that what you see? Anyway, I don't have Simulink so I can't run the sim() function.

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

카테고리

Help CenterFile Exchange에서 General Applications에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by