Search code examples
matlabfunctionexchange-serverfigures

Two figures in two different files - how to run first fig from the second one?


I have two figs in two different files. By clicking a button on first fig I want to show the second one... how to do this? is it possible?

If YES than how to exchange with data between two figures?


Solution

  • There are a number of ways to share data among GUIs. In general, you need to somehow make the graphics handle(s) from one GUI available to the other GUI so it can get/set certain object properties. Here's a very simple example that involves one GUI creating another and passing it an object handle:

    function gui_one
    
      hFigure = figure('Pos',[200 200 120 70],...  %# Make a new figure
                       'MenuBar','none');
      hEdit = uicontrol('Style','edit',...         %# Make an editable text box
                        'Parent',hFigure,...
                        'Pos',[10 45 100 15]);
      hButton = uicontrol('Style','push',...       %# Make a push button
                          'Parent',hFigure,...
                          'Pos',[10 10 100 25],...
                          'String','Open new figure',...
                          'Callback',@open_gui_two);
    
    %#---Nested functions below---
      function open_gui_two(hObject,eventData)
        gui_two(hEdit);  %# Pass handle of editable text box to gui_two
      end
    
    end
    
    %#---Subfunctions below---
    function gui_two(hEdit)
    
      displayStr = get(hEdit,'String');  %# Get the editable text from gui_one
      set(hEdit,'String','');            %# Clear the editable text from gui_one
      hFigure = figure('Pos',[400 200 120 70],...  %# Make a new figure
                       'MenuBar','none');
      hText = uicontrol('Style','text',...         %# Make a static text box
                        'Parent',hFigure,...
                        'Pos',[10 27 100 15],...
                        'String',displayStr);
    
    end
    

    After saving the above code to an m-file, you can create the first GUI by typing gui_one. You will see a small figure window with an editable text box and a button. If you type something in the text box, then hit the button, a second GUI will appear next to it. This second GUI uses the handle to the editable text box that is passed to it from the first GUI to get the text string, display it, and clear the string from the first GUI.

    This is just a simple example. For more information on programming GUIs in MATLAB, take a look at the MathWorks online documentation as well as the links in the answers to this SO question.