Solving Matlab for Loop Issue with di Index Value

  • Context: MATLAB 
  • Thread starter Thread starter Dustinsfl
  • Start date Start date
  • Tags Tags
    Loop Matlab
Click For Summary

Discussion Overview

The discussion revolves around a MATLAB coding issue related to the use of for loops and indexing when trying to store outputs from the 'stepinfo' function. Participants explore how to correctly index and store multiple outputs in a structured way, focusing on the behavior of loop variables and function outputs.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant expresses a desire to store outputs from 'stepinfo' in separate variables (d1 and d2) but encounters issues with overwriting data due to the way indexing is handled.
  • Another participant suggests using indexing with 'd(i)' or 'di(i)' to differentiate between outputs, but the original poster indicates that this approach did not resolve the issue.
  • There are questions about whether 'step' and 'stepinfo' are built-in functions, with confirmation that they are.
  • One participant proposes an alternative approach using conditional statements to assign values to d1 and d2 based on the value of ki, which is later confirmed to work by the original poster.
  • Concerns are raised about the structure of the for loop and the use of loop variables, with suggestions to redefine the indexing to avoid overwriting values.
  • Some participants express confusion about the intended functionality of the code and the logic behind the loop structure.

Areas of Agreement / Disagreement

Participants generally do not reach a consensus on the best approach initially, as multiple suggestions are made and some methods are reported to be ineffective. However, a later reply indicates that one proposed solution successfully resolved the issue.

Contextual Notes

There are unresolved questions regarding the proper use of loop variables and indexing in MATLAB, as well as the effectiveness of different proposed solutions. The discussion reflects varying levels of familiarity with MATLAB among participants.

Who May Find This Useful

This discussion may be useful for MATLAB users facing similar issues with loop indexing and output storage, particularly in the context of control systems and data analysis.

Dustinsfl
Messages
2,217
Reaction score
5
Code:
t = (0:0.0001:10);

for ki = [0.625, 1.125]
    kp = 1.25;
    H = tf([0, 0, 2*kp, 2*ki], [1, 3, 2 + 2*kp, 2*ki]);
    step(H, t)
    for i = [1, 2]
        di = stepinfo(H)
    end
    hold on
end
legend('kp = 0.625', 'kp = 1.125');

I want the di loop to return d1 = all the data and d2 = all the data but this isn't happening. It is just returning di = data and di = data. How can I get Matlab to prescribed the loop index value for the i? I believe it sees di as one term not d and i.
 
Physics news on Phys.org
Sounds like you want to index thus: d(i). 'di' will, as you suspect, be interpreted as a single symbol. Or, if you want the name of the vector to be di, you could do di(i). So you'd have di(1) = all the data, di(2) = all the data.
 
Ackbach said:
Sounds like you want to index thus: d(i). 'di' will, as you suspect, be interpreted as a single symbol. Or, if you want the name of the vector to be di, you could do di(i). So you'd have di(1) = all the data, di(2) = all the data.

I tried that already and it didn't work. It returns.

Code:
d = 

1x2 struct array with fields:

    RiseTime
    SettlingTime
    SettlingMin
    SettlingMax
    Overshoot
    Undershoot
    Peak
    PeakTimed = 

1x2 struct array with fields:

    RiseTime
    SettlingTime
    SettlingMin
    SettlingMax
    Overshoot
    Undershoot
    Peak
    PeakTime

instead of d1 and d2 plus the data doesn't appear
 
Is this all the code? Also, forgive me for not knowing MATLAB as well as I should, but are 'step' and 'stepinfo' built-in MATLAB functions, or did you write them?

Also, what's the big idea? What problem are you trying to solve?
 
Ackbach said:
Is this all the code? Also, forgive me for not knowing MATLAB as well as I should, but are 'step' and 'stepinfo' built-in MATLAB functions, or did you write them?

Also, what's the big idea? What problem are you trying to solve?

That is all the code.
step and stepinfo are built in
The idea is for the output of stepinfo to store two values of d1 and d2 of the required data.
The problem is solved. This is just an output of the data.

VHvXZif.png


This is the output. It calls both data sets d so only one saves since the last one is over written since d(i) doesn't work. Additionally, I noticed it running the second loop 4 times. Two times for each first loop. Not what I want either. I want it to have one di for the first run and one for the second. Therefore, I removed the second for loop but still don't know how to achieve the desired result.
 
Last edited:
You wouldn't normally do this, but if you only need to stuff the data into two variables, why not do

Code:
if ( ki <= 1 )
  d1 = stepinfo(H)
else
  d2 = stepinfo(H)
end if

I don't know if this is correct MATLAB syntax or not, but this idea should work. If you're more comfortable with using integers, you can initialize and increment an integer in addition to the ki variable.
 
I could be just ignorant of this type of structure, but this looks really weird to me. Usually when I code a "for" loop, the index variable will be increasing over the loop and used in the inner calculations. Here you using "ki" as an index and an inner variable that doesn't change it's name over the loop, so it's overwriting itself each time I believe. Maybe use a new index variable, say "a", and define "ki" in terms of "a", or just call it "k(a)".

Also in the inner loop you aren't using "i" at all so why would the value of "d" change? It seems to just call "d = stepinfo(H)" twice in a row and why would that change over the index i? H needs to change with the loop so each time is calls on "d = stepinfo(H)" there is a new value.

Hopefully there is something useful here for you and if not, sorry. I'm not following the idea of the code. I did a ton of Matlab last semester but now that I'm focusing on SAS I might be off here.

EDIT: Ah, ok I think I understand now. You want to conditionally run some code based on ki being between those two values right? That's what the outer loop is?
 
Jameson said:
I could be just ignorant of this type of structure, but this looks really weird to me. Usually when I code a "for" loop, the index variable will be increasing over the loop and used in the inner calculations. Here you using "ki" as an index and an inner variable that doesn't change it's name over the loop, so it's overwriting itself each time I believe. Maybe use a new index variable, say "a", and define "ki" in terms of "a", or just call it "k(a)".

Also in the inner loop you aren't using "i" at all so why would the value of "d" change? It seems to just call "d = stepinfo(H)" twice in a row and why would that change over the index i? H needs to change with the loop so each time is calls on "d = stepinfo(H)" there is a new value.

Hopefully there is something useful here for you and if not, sorry. I'm not following the idea of the code. I did a ton of Matlab last semester but now that I'm focusing on SAS I might be off here.

If you look at post one, you will see di. If you look at the explanation in my 4th post, you will see I removed it since it didn't work but the screenshot wasn't updated.

- - - Updated - - -

Ackbach said:
You wouldn't normally do this, but if you only need to stuff the data into two variables, why not do

Code:
if ( ki <= 1 )
  d1 = stepinfo(H)
else
  d2 = stepinfo(H)
end if

I don't know if this is correct MATLAB syntax or not, but this idea should work. If you're more comfortable with using integers, you can initialize and increment an integer in addition to the ki variable.

I use this method and it worked. Thanks.
 
dwsmith said:
I use this method and it worked. Thanks.

You're welcome!
 

Similar threads

Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 7 ·
Replies
7
Views
2K
Replies
2
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 18 ·
Replies
18
Views
6K
Replies
5
Views
7K