Saving for loop value after each iteration

5 ビュー (過去 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 Center および File ExchangeProgrammatic Model Editing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by