1
votes

I am using MatLab and I have two GUIs. When I click on a push button in one GUI, the second GUI will invoke, and both GUIs both can work in parallel. If any body knows answer to this question please give reply to me.

I have two GUI forms. In the first one I am rotating a line in a circle (by using the polar function. This is for my radar simulation purpose). In that GUI I have one push button. When I press it line (by using the for loop and pause function. Actually it's a simulation, kind of looks like a rotate in the circle)

The circle rotates until I press another pushbutton in the same GUI. I have one more push button. If I press this, it activates another GUI doing the same rotation but not the full circle, some part of the circle (sector). So here I need line in both circle and sector rotation. But actually what happens when I call the sector GUI (2nd GUI) from the circle GUI's pushbutton is that the line rotates in circle stops and control gives to sector after completion of the sector rotation. Circle is appearing in sector GUI.

If anybody knows how to execute these two GUIs in parallel, please answer me. If this is still too vague, please tell me and I will explain some more.

My code is below:

function twoguis
%Initializations: 
 hFigure2 = []; 
 hAxes2 = [];  
 %Make figure 1:
 hFigure1 = figure('Position',[50 200 300 300]);
 hAxes1 = axes('Parent',hFigure1,'Position',[0.1 0.2 0.8 0.7]); 
 hButton = uicontrol('Style','pushbutton',...              
                     'Position',[10 10 100 20],...       
                     'String','New Window',...  
                     'Callback',@button);
 % Start a loop that continuously changes the color of
 %   the axes at 1 second intervals: 
 while true,  % You will have to press Ctrl-c to stop!  
   newColor = rand(1,3); 
   set(hAxes1,'Color',newColor);
  if ishandle(hAxes2),
     set(hAxes2,'Color',newColor);
  end
    drawnow;
    pause(1); 
  end
 function button(source,event)  
   % Check if Figure 2 has already been made: 
  if ishandle(hFigure2), 
    return;    
  end
   % If it isn't made, make Figure 2:
   hFigure2 = figure('Position',[350 200 300 300]);
    hAxes2 = axes('Parent',hFigure2,'Position',[0.1 0.2 0.8 0.7]);  
    for xc=0:.05:6.28;
    polar([0,xc],[0,10]);
    pause(.1);
    end
   end 
 end

Can anyone suggest me how to change the color continuously and rotate the line in polar function continuously in two figures?

3
Make your question more explicit. It's not clear what you're asking about.Krzysztof Kozmic
It's still unclear what you are asking. Perhaps you can edit your question and add the code you have so far so we can see exactly what you are doing. My best guess is that you have two GUIs with circular dials that you want to update simultaneously... is that correct?gnovice
yes exacltly what u said if u can do , please give the answer for thatTatiparthi Kiran

3 Answers

9
votes

I made a video that covers how to make two or more GUIs share data and work together. The short answer is use SETAPPDATA and GETAPPDATA to share data between GUI's. The long answer is here:

http://blogs.mathworks.com/videos/2005/10/03/guide-video-part-two/

My collection of GUI videos can be found here:

http://blogs.mathworks.com/videos/category/gui-or-guide/

-Doug

3
votes

EDIT: I know you wanted answers for how to do this in GUIDE, but maybe you will find this non-GUIDE, nested-function answer helpful...

The following code creates a radar GUI window with a polar plot and 2 buttons. The "Start" button will begin rotating the line counter-clockwise, and the button will then turn into a "Stop" button (which will stop rotating the line if pressed again). The second button launches a sector GUI. This figure has a polar plot whose line will rotate through a section of the polar plot, within a range of 45 degrees on either side of the current position of the radar GUI line. There is another "Start"/"Stop" button in the sector GUI to control the animation. While the sector GUI is open, the radar GUI does not animate. Once the sector GUI is closed, the radar GUI can rotate again. You can only open 1 sector GUI at a time.

function radar_gui

  % Initializations:

  radarAngle = 0;      % Current angle of radar GUI
  sectorAngle = 0;     % Current Angle of sector GUI
  radarStep = pi/90;   % Angle increment (radians) per 0.1 s
  sectorWidth = pi/2;  % Angle (radians) swept by sector GUI
  hSectorFigure = [];
  hSectorAxes = [];
  hSectorLine = [];
  hButton2 = [];

  % Make radar figure:

  hRadarFigure = figure('Position',[50 200 300 300],...
                        'DeleteFcn',@delete_timer);
  hRadarAxes = axes('Parent',hRadarFigure,...
                    'Position',[0.1 0.2 0.8 0.7]);
  hRadarLine = polar(hRadarAxes,[0 radarAngle],[0 1]);
  hButton1 = uicontrol('Style','pushbutton',...
                       'Parent',hRadarFigure,...
                       'Position',[10 10 60 20],...
                       'String','Start',...
                       'Callback',@toggle_radar);
  uicontrol('Style','pushbutton','Parent',hRadarFigure,...
            'Position',[190 10 100 20],...
            'String','Show Sector',...
            'Callback',@open_sector);

  % Create a timer that spins the radar lines:

  spinTimer = timer('TimerFcn',@radar_timer,...
                    'ExecutionMode','fixedRate',...
                    'Period',0.1,...
                    'TasksToExecute',inf);

%~~~Begin nested functions~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  function open_sector(source,event)
    if ishandle(hSectorFigure),
      return;
    end
    sectorAngle = radarAngle;
    hSectorFigure = figure('Position',[350 200 300 300]);
    hSectorAxes = axes('Parent',hSectorFigure,...
                       'Position',[0.1 0.2 0.8 0.7]);
    hButton2 = uicontrol('Style','pushbutton',...
                         'Parent',hSectorFigure,...
                         'Position',[10 10 60 20],...
                         'String',get(hButton1,'String'),...
                         'Callback',@toggle_radar);
    hSectorLine = polar(hSectorAxes,[0 sectorAngle],[0 1]);
    drawnow;
  end

  function toggle_radar(source,event)
    if strcmp(get(source,'String'),'Start'),
      set(hButton1,'String','Stop');
      if ishandle(hButton2),
        set(hButton2,'String','Stop');
      end
      start(spinTimer);
    else
      set(hButton1,'String','Start');
      if ishandle(hButton2),
        set(hButton2,'String','Start');
      end
      stop(spinTimer);
    end
    drawnow;
  end

  function radar_timer(source,event)
    if ishandle(hSectorLine),
      sectorAngle = sectorAngle+radarStep;
      if (sectorAngle >= radarAngle+sectorWidth/2),
        sectorAngle = radarAngle-sectorWidth/2;
      end
      set(hSectorLine,'XData',[0 cos(sectorAngle)],...
                      'YData',[0 sin(sectorAngle)]);
    else
      radarAngle = radarAngle+radarStep;
      if (radarAngle >= 2*pi),
        radarAngle = radarAngle-2*pi;
      end
      set(hRadarLine,'XData',[0 cos(radarAngle)],...
                     'YData',[0 sin(radarAngle)]);
    end
    drawnow;
  end

  function delete_timer(source,event)
    stop(spinTimer);
    delete(spinTimer);
    if ishandle(hSectorFigure),
      delete(hSectorFigure);
    end
  end

%~~~End nested functions~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

end

If you want to adjust the speed of rotation or sweep angle of the sector GUI, you can adjust the values for the "radarStep" and "sectorWidth" variables at the beginning of the function. Hope this helps!

1
votes

The problem is that the MATLAB M-code interpreter is basically single-threaded. So when the function "button" gets called, it takes control of the interpreter and doesn't give it back until it has finished a sweep. I suggest looking into using the MATLAB timer class. This gives a bit more of a multithreaded "feel", though technically when the timer callback is called, it too blocks other M-code from running. Here is a modified form of your original code that shows what I am talking about:

function twoguis
%Initializations: 
    hFigure2 = []; 
    hAxes2 = [];  
    %Make figure 1:
    hFigure1 = figure('Position',[50 200 300 300]);
    hAxes1 = axes('Parent',hFigure1,'Position',[0.1 0.2 0.8 0.7]); 
    hButton = uicontrol('Style','pushbutton',...              
                        'Position',[10 10 100 20],...       
                        'String','New Window',...  
                        'Callback',@button);
    % Start a loop that continuously changes the color of
    %   the axes at 1 second intervals: 
    while true,  % You will have to press Ctrl-c to stop!  
        newColor = rand(1,3); 
        set(hAxes1,'Color',newColor);
        if ishandle(hAxes2),
            set(hAxes2,'Color',newColor);
        end
        drawnow;
        pause(1); 
    end

    function button(source,event)  
    % Check if Figure 2 has already been made: 
        if ishandle(hFigure2), 
            return;    
        end
        % If it isn't made, make Figure 2:
        hFigure2 = figure('Position',[350 200 300 300]);
        hAxes2 = axes('Parent',hFigure2,'Position',[0.1 0.2 0.8 0.7]);  
        tmr = timer('TimerFcn',@spin, 'executionmode','fixedrate','period',.1);
        start(tmr)

        function spin(obj, event)
            polar(hAxes2,[0,get(obj, 'TasksExecuted')*0.05],[0,10]);
        end
    end 
end