Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
stop a simple loop

Subject: stop a simple loop

From: Hermano Cappa

Date: 4 Mar, 2010 10:19:05

Message: 1 of 4

Hey,

I have a simple for-loop wherein I calculate a value in a function, but this value also determines the stop condition of the for-loop. My question is: how can I formulate the loop so it will repeat until the condition reaches the calculated value. A simple example:

for i=1:value
   ......
   %FUNCTION
   value=calc_value()
   ............
   ............
end

function value = calc_value()
   x = 5;
   y = 1;
   value = x+y
end

Of course this doesn't work because the value is not known at the begin of the for loop.

Subject: stop a simple loop

From: Lenny

Date: 4 Mar, 2010 10:48:06

Message: 2 of 4

use
- a WHILE loop, or
- if value==myvalue, RETURN; end

br _lenny

"Hermano Cappa" <cappasteven@gmail.com> wrote in message <hmo1ep$2qf$1@fred.mathworks.com>...
> Hey,
>
> I have a simple for-loop wherein I calculate a value in a function, but this value also determines the stop condition of the for-loop. My question is: how can I formulate the loop so it will repeat until the condition reaches the calculated value. A simple example:
>
> for i=1:value
> ......
> %FUNCTION
> value=calc_value()
> ............
> ............
> end
>
> function value = calc_value()
> x = 5;
> y = 1;
> value = x+y
> end
>
> Of course this doesn't work because the value is not known at the begin of the for loop.

Subject: stop a simple loop

From: ImageAnalyst

Date: 4 Mar, 2010 12:47:53

Message: 3 of 4

Use the break command

for i=1:value
    ......
    % Call the calc_value() function:
    value = calc_value()
    % Check its returned value and bail out if necessary.
    if value == terminationValue
        break; % Exits out of the for loop
    end
    ............
end

Subject: stop a simple loop

From: ImageAnalyst

Date: 4 Mar, 2010 12:56:27

Message: 4 of 4

Sorry - I didn't look closely enough - you would need to set the
ending value to be some high number (to prevent infinite loop) if you
want to use the for/break method, rather than put value in the for
loop iterator. Or even better, use a while loop. Perhaps something
like this:

value = terminationValue + 1; % Set terminationValue to whatever your
condition needs, in advance.
tolerance = eps('single'); % To handle floating point - or you can
use whatever tolerance you want.
while abs(value - terminationValue) > tolerance
    ......
    % Call the calc_value() function:
    value = calc_value()
    ............
end

To understand why I used a tolerance instead of equality, check out
the FAQ:
http://matlabwiki.mathworks.com/MATLAB_FAQ#Why_is_0.3-0.2-0.1_not_equal_to_zero_.28or_similar.29.3F

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us