Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

How to get the last variable of a for loop in MATLAB

  1. Sep 8, 2016 #1
    Aloha guys,

    I am just having trouble getting MatLab to output only the last variable of my "for loop."
    If anyone can help all I am looking for is an example of notation.
    p.s. I have done this before just not in a while and will continue to look through my old scripts and functions. just not in a while.
    Thank you,
    HWN Phisics Nerd :P
  2. jcsd
  3. Sep 8, 2016 #2


    Staff: Mentor

    Do you mean the last value of your loop control variable?

    If so, here's how you would do it in C, which should be fairly close to what it would be in matlab.

    Code (C):
    int N = 10;
    int i;

    for (i = 0; i < N + 1; i++)
       if ( i == N) printf("Last value is %d", i);
  4. Sep 8, 2016 #3


    Staff: Mentor

Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted

Similar Discussions: How to get the last variable of a for loop in MATLAB
  1. MATLAB - Simple loop? (Replies: 6)