MATLAB Solving Matlab for Loop Issue with di Index Value

  • Thread starter Thread starter Dustinsfl
  • Start date Start date
  • Tags Tags
    Loop Matlab
AI Thread Summary
The discussion revolves around a MATLAB coding issue where the user is trying to store the output of the `stepinfo` function into two separate variables, `d1` and `d2`, based on the value of `ki`. The original code structure causes the data to be overwritten, leading to only one set of results being saved. Suggestions include using conditional statements to assign values to `d1` and `d2` based on the `ki` variable, and the importance of properly indexing variables to avoid overwriting. The user initially attempted to use a loop but found it ineffective, as it did not yield the desired results. Ultimately, they found success with a simpler conditional approach, confirming that this method worked for their needs.
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

Back
Top