Saving for loop value after each iteration

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 个)

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 个评论

@Image Analyst Thank you for the response but it is giving error that says Arrays have incompatible sizes
You're not showing us something. Not only are you not showing us the complete error (ALL the red text), but you're not even showing us the code. There is nothing in the code I gave that would cause that specific error. Nothing.
  1. Assigning the whole array resistance to a variable R would not throw that error.
  2. Whatever sim() returns, it's put into a new variable simOut (that is overwritten on each iteration) so that would not cause the error.
  3. You assigning the i'th cell of a cell array to the results of find(). Cell arrays can take anything so that wouldn't cause the error. Though I wonder if you really want ismember() or something else since find() does not take a character array as its second input - so this leads me to believe again that this is not your actual code. You can even put another cell array into a cell array so the sizes don't matter. See the FAQ:
So that means you're running some other code that is not shown here. Of course I cannot comment on any code you are not showing us.
@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.
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.

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 General Applications 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by