Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>For an "infinite" loop that can still be easily stopped when a certain condition is met, you can set up your <a href="https://www.mathworks.com/help/matlab/ref/while.html" rel="nofollow noreferrer">while condition</a> to be a <a href="https://www.mathworks.com/help/matlab/logical-operations.html" rel="nofollow noreferrer">logical variable</a> (i.e. flag) that can be updated within your loop:</p> <pre><code>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 </code></pre> <p>A while loop can also be terminated if a <a href="https://www.mathworks.com/help/matlab/ref/break.html" rel="nofollow noreferrer"><code>break</code></a> or <a href="https://www.mathworks.com/help/matlab/ref/return.html" rel="nofollow noreferrer"><code>return</code></a> command is encountered within the loop.</p> <p><br> <strong>EXAMPLE:</strong></p> <p>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 <a href="https://www.mathworks.com/help/matlab/ref/uicontrol.html" rel="nofollow noreferrer">push button</a> or pressing <kbd>q</kbd> while the figure window has focus (using the <a href="https://www.mathworks.com/help/matlab/ref/matlab.ui.figure-properties.html#property_d119e285730" rel="nofollow noreferrer"><code>'KeyPressFcn'</code> property</a> 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:</p> <pre><code>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 </code></pre> <p>The above example makes use of <a href="https://www.mathworks.com/help/matlab/matlab_prog/nested-functions.html" rel="nofollow noreferrer">nested functions</a> so that the <code>'KeyPressFcn'</code> and button callback can access and modify the value of <code>keepLooping</code> in the workspace of the <code>stop_watch</code> function.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload