tags:

views:

2057

answers:

4

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?

+2  A: 

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!

gnovice
Dear gnovice my problem is not related to parallel computing tool box i am again explain the problem some more clearly i have 1 gui where i plotted the polar in that polar centre to edge draw aline that line rotated in that circle
when i call second gui from this gui through push putton another gui run parallely with first second also same functioality both to work parallely if you know please help me
Dear gnovice almost the problem is same as you explained ,can you give the same code by using the guide,I think it is through programatically,will give the code in thorug guide because it conguses me but the output of above code is running but i need this through guide gui thankyou mr.gnovice
Unfortunately, I never use GUIDE. If you need help with doing things in GUIDE, I would suggest Doug's videos. Also, I'm not sure of how you want your GUIs to update... do you want them to update continuously when the first figure opens, or do you want it to start when the second one opens?
gnovice
thanq,just add the code u given to me after hAxes2 = axes('Parent',hFigure2,'Position',[0.1 0.2 0.8 0.7]); in button function with code for x=0:.05:6.28; polar([0,x],[0,10]); pause (.1); end if you do this ,you will easy to understand my problem, what i want in this is i wnat is colors in first gui
and at the same time line rotating in polar in second gui parallel, in actuval my problem i have polars in each gui and rotate boht parallel , i hope u can understan my problem if u still unclear please tell to me i will do another explanation to understand u much more clearthanq
+7  A: 

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

MatlabDoug
Oops! I neglected to link to your blog in my answer. I knew there were some MathWorks folks who gave examples of this. =)
gnovice
Dear doug my problem is not related to parallel computing tool box i am again explain the problem some more clearly i have 1 gui where i plotted the polar in that polar centre to edge draw aline that line rotated in that circle
when i call second gui from this gui through push putton another gui run parallely with first second also same functioality both to work parallely if you know please help me
Tatiparthi,I never mentioned the parallel computing toolbox.-Doug
MatlabDoug
A: 

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
SCFrench
Thanq french can u do one more favour for me ,will you rotate line in polar function in first figure also at the same time when i press button in secon figure polar function also works parallel to the figure 1 if you the answer please tell to me. thank you
sir,if you have any idea about guide please give the code through guide or other wise five the code given by you in above ,give that code through guide
A: 

Hello SCFrench and everyone. I try to run another function (cos(x)) in figure 2, but I don't know how I can keep the process only in the figure 2. Because, when I move my mouse to the figure 1, the process is stopped in figure 2 and it is translate to the figure 1. I would like to get the same effect as the polar function in figure 2 (it was great): The radar is running in figure 2, independiently if a move the mouse to figure 1.

Thanks in advance.

Here you are the code, waiting for being modified:

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)

% my modification to try to run a new function (cos(x)) in figure 2 %polar(hAxes2,[0,get(obj, 'TasksExecuted')*0.05],[0,10]);

i=0; x=0;

while 1 i=i+1; xdin=i*0.1; drawnow x=[x xdin]; y=cos (x); plot(x,y)

end end end end