Solving Matlab for Loop Issue with di Index Value

  • MATLAB
  • Thread starter Dustinsfl
  • Start date
  • Tags
    Loop Matlab
In summary: You wouldn't normally do this, but if you only need to stuff the data into two variables, why not do if ( ki <= 1 ) d1 = stepinfo(H)else d2 = stepinfo(H)end ifI 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
  • #1
Dustinsfl
2,281
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
  • #2
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.
 
  • #3
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
 
  • #4
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?
 
  • #5
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:
  • #6
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.
 
  • #7
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?
 
  • #8
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.
 
  • #9
dwsmith said:
I use this method and it worked. Thanks.

You're welcome!
 

1. What is a for loop in Matlab?

A for loop in Matlab is a programming construct that allows you to repeat a specific set of instructions for a specified number of times. It is often used to iterate through a data set or perform a repetitive task.

2. What is the di index value in a for loop?

The di index value in a for loop represents the current iteration of the loop. It starts at 1 and increments by 1 with each iteration until it reaches the specified number of iterations.

3. How do I solve for loop issues with di index value in Matlab?

To solve for loop issues with di index value in Matlab, you can check that the loop is properly initialized with the correct starting and ending values. You can also use the debugger to step through each iteration and check the value of di at each step.

4. Can I use a di index value outside of a for loop in Matlab?

Yes, you can use a di index value outside of a for loop in Matlab. However, it will only have the value of the last iteration of the loop. If you need to use the di index value outside of the loop, you can assign it to a variable and use that variable instead.

5. How can I improve the efficiency of my for loop in Matlab?

To improve the efficiency of your for loop in Matlab, you can try to vectorize your code instead of using a loop. This means performing operations on entire arrays instead of individual elements. You can also preallocate memory for your arrays before the loop to save time on each iteration.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
941
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
943
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
18
Views
5K
Back
Top