Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I loop indefinitely, but stop on some condition(s)?

Tags:

loops

matlab

I am working on a project in MATLAB. It includes continuous plotting of a data about temperature received from the serial port of the computer. I want to do it infinitely so is there any way to create infinite loops like in C?

Now if is implemented as:

while(true)
%comments
end;

as Moore stated below, then is there any way to update the flags so that it could be terminated as per requirement or any other operation?

Example: I am plotting the data for my 5 nodes communicating through ZigBee then if ones I have selected to plot 4 nodes on Axis then is there any way after starting the infinite loop so that I could change the data being used inside the loop through an input method of GUI of MATLAB or any flag?

like image 653
skjoshi Avatar asked Dec 19 '10 05:12

skjoshi


2 Answers

For an "infinite" loop that can still be easily stopped when a certain condition is met, you can set up your while condition to be a logical variable (i.e. flag) that can be updated within your loop:

keepLooping = true;   % A flag that starts as true
while keepLooping
  % Read, process, and plot your data here
  keepLooping = ...;  % Here you would update the value of keepLooping based
                      %   on some condition
end

A while loop can also be terminated if a break or return command is encountered within the loop.


EXAMPLE:

As an example of some of the GUI-based ways you can stop a loop, here is a program that creates a simple GUI that continuously increments and displays a counter once every second using a while loop. The GUI has two ways to stop the loop: a push button or pressing q while the figure window has focus (using the 'KeyPressFcn' property of the figure to run code when a key is pressed). Just save this code in an m-file somewhere on the MATLAB path and run it to test the example:

function stop_watch

  hFigure = figure('Position', [200 200 120 70], ...       % Create a figure window
                   'MenuBar', 'none', ...
                   'KeyPressFcn', @stop_keypress);
  hText = uicontrol(hFigure, 'Style', 'text', ...          % Create the counter text
                    'Position', [20 45 80 15], ...
                    'String', '0', ...
                    'HorizontalAlignment', 'center');
  hButton = uicontrol(hFigure, 'Style', 'pushbutton', ...  % Create the button
                      'Position', [20 10 80 25], ...
                      'String', 'Stop', ...
                      'HorizontalAlignment', 'center', ...
                      'Callback', @stop_button);
  counter = -1;
  keepLooping = true;
  while keepLooping       % Loop while keepLooping is true
    counter = counter+1;  % Increment counter
    set(hText, 'String', int2str(counter));  % Update the counter text
    pause(1);             % Pause for 1 second
  end

%---Begin nested functions---

  function stop_keypress(hObject, eventData)
    if strcmp(eventData.Key, 'q')            % If q key is pressed, set
      keepLooping = false;                   %   keepLooping to false
    end
  end

  function stop_button(hObject, eventData)
    keepLooping = false;                     % Set keepLooping to false
  end

end

The above example makes use of nested functions so that the 'KeyPressFcn' and button callback can access and modify the value of keepLooping in the workspace of the stop_watch function.

like image 114
gnovice Avatar answered Nov 15 '22 21:11

gnovice


while (true)
    % block of code here
end
like image 28
Reese Moore Avatar answered Nov 15 '22 21:11

Reese Moore