Saving for loop value after each iteration

5 Ansichten (letzte 30 Tage)
Sugar Mummy
Sugar Mummy am 20 Jan. 2022
Kommentiert: Image Analyst am 20 Jan. 2022
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

Antworten (1)

Image Analyst
Image Analyst am 20 Jan. 2022
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 Kommentare
Sugar Mummy
Sugar Mummy am 20 Jan. 2022
@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 am 20 Jan. 2022
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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Programmatic Model Editing finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by