如何在MatLab中并行运行两个GUI?

时间:2009-02-26 09:18:25

标签: user-interface matlab

我正在使用MatLab,我有两个GUI。当我在一个GUI中单击按钮时,第二个GUI将调用,并且两个GUI都可以并行工作。如果有任何机构知道这个问题的答案,请回复我。

我有两个GUI表单。在第一个中,我在一个圆圈中旋转一条线(通过使用极性函数。这是为了我的雷达模拟目的)。在那个GUI中我有一个按钮。当我按下它时(通过使用for循环和暂停功能。实际上它是一个模拟,看起来像是在圆圈中旋转)

圆圈旋转,直到我在同一个GUI中按下另一个按钮。我还有一个按钮。如果按此按钮,它会激活另一个执行相同旋转的GUI,但不会激活整圆,圆圈的一部分(扇区)。所以在这里我需要在圆圈和扇区旋转中都行。但实际上,当我从圆形GUI的按钮调用扇区GUI(第二个GUI)时,会发生这样的情况:线条以圆形停止旋转,控制在扇区旋转完成后给扇区。 Circle出现在扇区GUI中。

如果有人知道如何并行执行这两个GUI,请回答我。如果这仍然太模糊,请告诉我,我会解释一些。

我的代码如下:

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

有谁能建议我如何连续改变颜色并在两个数字中连续旋转极性函数线?

3 个答案:

答案 0 :(得分:9)

我制作了一个视频,介绍了如何让两个或多个GUI共享数据并协同工作。简短的回答是使用SETAPPDATA和GETAPPDATA在GUI之间共享数据。答案很长:

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

我可以在此处找到我的GUI视频集:

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

-Doug

答案 1 :(得分:3)

编辑:我知道你想要在GUIDE中如何做到这一点的答案,但也许你会发现这个非GUIDE,嵌套功能的答案很有帮助......

以下代码创建一个带有极坐标图和2个按钮的雷达GUI窗口。 “开始”按钮将开始逆时针旋转线,然后按钮将变为“停止”按钮(如果再次按下,将停止旋转线)。第二个按钮启动扇区GUI。该图具有极坐标图,其线将在极坐标图的一部分中旋转,在雷达GUI线的当前位置的任一侧上的45度范围内。扇区GUI中还有另一个“开始”/“停止”按钮来控制动画。当扇区GUI打开时,雷达GUI不具有动画效果。一旦扇区GUI关闭,雷达GUI就可以再次旋转。您一次只能打开1个扇区GUI。

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

如果要调整扇区GUI的旋转速度或扫描角度,可以在函数开头调整“radarStep”和“sectorWidth”变量的值。希望这有帮助!

答案 2 :(得分:1)

问题是MATLAB M代码解释器基本上是单线程的。因此,当调用函数“button”时,它会控制解释器,并且在完成扫描之前不会将其返回。我建议考虑使用MATLAB计时器类。这提供了更多的多线程“感觉”,虽然从技术上讲,当调用定时器回调时,它也会阻止其他M代码运行。以下是原始代码的修改形式,显示了我正在谈论的内容:

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