Izzo
Izzo

Reputation: 4928

Creating a MATLAB GUI for an infinite looping script

I'm currently working on a MATLAB script call manager.m . This script contains an infinite loop that monitors a communication thread for incoming commands.

I would like to a create a status window for this program such that a user knows that it is function properly (e.g. indicate good connection status, current state, etc.). Note that I do not want a user to be able to input any information from this GUI, it's only purpose is to be a display.

I went ahead and created a simple GUI using the App Designer program. I converted my manager.m script into a function that is called during the GUI start-up.

However, I'm running into an issue that since the function contains an infinite loop, closing the GUI does not actually kill the function. Essentially the function just keeps looping/executing.

After several failed attempts to fix the problem, I feel like I'm not using the GUI App Designer as it would typically be used. It seems like apps are responsible for the looping aspect, and simply monitor user inputs for execute events when necessary. By adding my own infinite loop into a manager function, it seems to be complicating everything.

So my question: if I want to create a status display for an infinite looping MATLAB script, what is the preferred method for doing this?

Upvotes: 1

Views: 488

Answers (2)

Izzo
Izzo

Reputation: 4928

After thinking about it a little more, I realized that the MATLAB App Designer could just be used as a "designer". I went ahead and created my display screen, and then clicked on the "View Code" option.

If you scroll down through the code, you can find section that initializes all of the user-interface components. You can simply copy this code and paste it into your own script.

This method allows you to use the MATLAB drag-and-drop designer to generate the user interface code, while allowing you control your own program flow through your script.

Upvotes: 0

Luna
Luna

Reputation: 76

In case you just want to end the loop, you can use the following solution:

  1. Add to the figure close a setappdata(0, 'do_end_loop', 1); command as follows:

    function figure1_CloseRequestFcn(hObject, eventdata, handles)
    % hObject    handle to figure1 (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    
    setappdata(0, 'do_end_loop', 1);
    
    % Hint: delete(hObject) closes the figure
    delete(hObject);
    
  2. In the infinite loop, use do_end_loop = getappdata(0, 'do_end_loop');, and add some logic to break the loop.
    The following code sample is an example for an infinite loop that do some work, and breaks when GUI is closed by the user:

    setappdata(0, 'do_end_loop', 0);
    
    counter = 0;
    while (1)
        do_end_loop = getappdata(0, 'do_end_loop');
    
        if (do_end_loop)
            break;
        end   
    
        handles.text1.String = num2str(counter);
        counter = counter + 1;
    
        pause(0.1);
    end
    

(I placed the loop in a callback of a pushbutton).

Upvotes: 2

Related Questions