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
SUMMARY

The forum discussion addresses a MATLAB coding issue regarding the proper indexing of output data from the 'stepinfo' function within a for loop. The user initially attempted to store results in a variable named 'di', which was interpreted as a single symbol rather than an indexed array. The solution proposed involves using conditional statements to assign outputs to 'd1' and 'd2' based on the value of 'ki'. This method effectively resolves the issue of overwriting data and ensures that the desired outputs are correctly stored.

PREREQUISITES
  • Understanding of MATLAB syntax and functions
  • Familiarity with control flow structures in programming (if-else statements)
  • Knowledge of MATLAB's 'step' and 'stepinfo' functions
  • Basic concepts of transfer functions in control systems
NEXT STEPS
  • Explore MATLAB conditional statements for data assignment
  • Learn about MATLAB for loops and indexing techniques
  • Investigate MATLAB's 'step' and 'stepinfo' functions in detail
  • Study transfer function representation and analysis in control systems
USEFUL FOR

MATLAB users, control systems engineers, and anyone looking to improve their programming skills in MATLAB, particularly in handling output data from functions.

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
1K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · 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
  • · Replies 8 ·
Replies
8
Views
2K