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

AI Thread Summary
The discussion centers around how to get MATLAB to output only the last variable from a "for loop." A user seeks clarification on the correct notation for achieving this. Responses suggest that the user may be looking for the last value of the loop control variable. Examples are provided, including a C-style approach and a MATLAB-specific method. The MATLAB example shows how to store the last value in a variable during the loop, ensuring that it captures the final iteration's value. Additional resources, such as MATLAB documentation on loop control statements, are also referenced for further guidance.
KonaGorrila
Messages
12
Reaction score
0
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
 
Physics news on Phys.org
KonaGorrila said:
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.
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.

C:
int N = 10;
int i;

for (i = 0; i < N + 1; i++)
{
   if ( i == N) printf("Last value is %d", i);
}
 

Similar threads

Replies
4
Views
2K
Replies
2
Views
2K
Replies
5
Views
3K
Back
Top