Introduction

Matlab Logo Oftentimes, we need to share data between two separate GUIs. This tutorial will show you how to do just that. While there are many ways to share data between two GUIs, this tutorial will present the most simple method (in our opinion). Below are two simple GUIs that were created as an example for this tutorial.

Quan’s GUI and Daniel’s GUI each take in an input. Quan’s GUI is able to extract the input from Daniel’s GUI, and vice versa. How is this accomplished? Let’s run through the example and find out.

Note: If you want to use a sub GUI to change settings parameters on a main GUI, check out this tutorial instead!

Quan's GUIDaniel's GUI

The Example

  1. First, download the GUI skeletons here. Unzip the files and place them wherever you please.

  2. Now, type guide at the command prompt.

    Command prompt

  3. Choose to open the sample GUIs by clicking on “Open Existing GUI”. Click on “Browse” to locate where you saved the GUI files.

    GUIDE Screen

  4. The two GUIs should look like this when you open them:

    Quan's GUI Daniel's GUI

  5. Click on the mfile icon icon on the GUI figure to bring up the accompanying .m file.

  6. First, lets add the following code to pushbutton1_Callback for Quan’s GUI.

    danielFigureHandle  = daniel; %stores the figure handle of Daniel's GUI here
     
    %stores the GUI data from Daniel's GUI here
    %now we can access any of the data from Daniel's GUI!!!!
    danielData = guidata(danielFigureHandle); 
     
    %store the input text from Daniel's GUI 
    %into the variable daniel_input
    daniel_input = get(danielData.editText_Daniel,'String');
     
    %set the static text on Quan's GUI to match the
    %input text from Daniel's GUI
    set(handles.display_daniel_input,'String',daniel_input);
     
    %notice that danielData is the structure containing the data from Daniel's GUI
    %notice that handles is the structure containing data from Quan's GUI,
    %which is the local GUI (i.e., the GUI that this function is running from)
  7. Second, lets add the following code to pushbutton1_Callback for Daniel’s GUI.

    quanFigureHandle  = quan;  %stores the figure handle of Quan's GUI here
     
    %stores the GUI data from Quan's GUI here
    %now we can access any of the data from Quan's GUI!!!!
    quanData = guidata(quanFigureHandle); 
     
    %store the input text from Quan's GUI 
    %into the variable quan_input
    quan_input = get(quanData.editText_Quan,'String');
     
    %set the static text on Daniel's GUI to match the
    %input text from Quan's GUI
    set(handles.display_quan_input,'String',quan_input);
     
    %notice that quanData is the structure containing the data from Quan's GUI
    %notice that handles is the structure containing data from Daniel's GUI,
    %which is the local GUI (i.e., the GUI that this function is running from)
  8. And that’s it! Run the two GUIs simultaneously side by side. Enter in any input you choose for the two GUIs and test it to make sure that it does what you expect it too.

Download the Source Files

You can download the source files here.