注册 | 登录

解决user interface - Matlab GUI select which axes to plot

itPublisher 分享于



I am using the code below to plot data from the serial port. Since I have two axes for plotting, how can I select a particular axes for this plot? From similar problem, I found that they use axes(handles.axes2);. Since I have the plot declared at the start of the program, where should I place this line of code? I tried placing it before specifying the plot title etc. but it is not working.

% Serial Data Logger
% Yu Hin Hau
% 7/9/2013


%User Defined Properties 
serialPort = 'COM5';            % define COM port #
plotTitle = 'Serial Data Log';  % plot title
xLabel = 'Elapsed Time (s)';    % x-axis label
yLabel = 'Data';                % y-axis label
plotGrid = 'on';                % 'off' to turn off grid
min = -1.5;                     % set y-min
max = 1.5;                      % set y-max
scrollWidth = 10;               % display period in plot, plot entire data log if <= 0
delay = .01;                    % make sure sample faster than resolution

%Define Function Variables
time = 0;
data = 0;
count = 0;

%Set up Plot
plotGraph = plot(time,data,'-mo',...
                'MarkerFaceColor',[.49 1 .63],...

axis([0 10 min max]);

%Open Serial COM Port
s = serial(serialPort)
disp('Close Plot to End Session');


while ishandle(plotGraph) %Loop when Plot is Active

    dat = fscanf(s,'%f'); %Read Data from Serial as Float

    if(~isempty(dat) && isfloat(dat)) %Make sure Data Type is Correct        
        count = count + 1;    
        time(count) = toc;    %Extract Elapsed Time
        data(count) = dat(1); %Extract 1st Data Element         

        %Set Axis according to Scroll Width
        if(scrollWidth > 0)
        set(plotGraph,'XData',time(time > time(count)-scrollWidth),'YData',data(time > time(count)-scrollWidth));
        axis([time(count)-scrollWidth time(count) min max]);
        axis([0 time(count) min max]);

        %Allow MATLAB to Update Plot

%Close Serial COM Port and Delete useless Variables
clear count dat delay max min plotGraph plotGrid plotTitle s ...
        scrollWidth serialPort xLabel yLabel;

disp('Session Terminated...');
matlab user-interface matlab-figure
  this question
edited Apr 4 '16 at 1:31 Andras Deak 13.3k 4 22 50 asked Mar 30 '16 at 13:20 TheBeginner 21 4      As a side note, you can use drawnow rather than pause to force a re-draw –  Suever Mar 30 '16 at 14:24


1 Answers


The trick to get reliable plotting and manipulation is to always specify the parent explicitly using the Parent parameter when creating a plot or any other graphics object. All graphics objects support this parameter.

hax = axes();
plot(x,y, 'Parent', hax);

The other alternative, as suggested by @matlabgui is to specify the parent axes as the first input to plot:

plot(hax, x, y);

I personally prefer to use the Parent parameter as a parameter value pair though, as that behavior is consistent across all graphics objects.

You should also specify the axes handle when using other functions which operate on an axes.

xlabel(hax, 'XLabel')
ylabel(hax, 'YLabel')
title(hax, 'This is a title')
axis(hax, [0 0 1 1])
grid(hax, 'on')
hold(hax, 'on')

This is particularly important if you are dealing with an interactive GUI as the user could easily click on a different axes in the middle of your plotting causing the value of gca to change unexpectedly. Also changing the current axes (using axes(hax)) can cause a poor user experience.


For your specific code, this would involve changing your initial plot call:

plotGraph = plot(time,data,'-mo',...
                 'MarkerFaceColor',[.49 1 .63],...
                 'MarkerSize',2, ...
                 'Parent', handles.axes2);

I would also recommend adding explicit axes handles to your calls to: grid, title, axis, xlabel, and ylabel to ensure that their target is the axes you want.

  this answer
edited Mar 30 '16 at 13:32 answered Mar 30 '16 at 13:23 Suever 43.7k 12 36 56      The plot command accepts the parent as the 1st argument so you can also do plot ( hax, x, y ) –  matlabgui Mar 30 '16 at 13:27      @matlabgui Yes that's true. I just prefer to always specify it as a param/value since that it consistent across all graphics. –  Suever Mar 30 '16 at 13:28      @TheBeginner It looks ok but look at my examples above for xlabel, axis, etc. Particularly for axis and grid you need to specify the axes handle as the first input rather than the 'Parent', hax format. –  Suever Mar 30 '16 at 14:26      @Suever I did the following. Is it correct plotGraph = plot(time,data,'Parent', handles.axes6); title(plotTitle,'FontSize',25,'Parent', handles.axes6); xlabel(xLabel,'FontSize',15,'Parent', handles.axes6); ylabel(yLabel,'FontSize',15,'Parent', handles.axes6); axis([0 10 min max],'Parent', handles.axes6); grid(plotGrid,'Parent', handles.axes6); . An arror is showing for the axis and grip. What am I doing wrong? Also, when plotting other graphs, do I use the same format, i.e ....Parent', handles.axes3) ? –  TheBeginner Mar 30 '16 at 14:29      @TheBeginner see my comment just above yours about axis and grid. Also, yes, this will work for all plots. –  Suever Mar 30 '16 at 14:30









您的注册邮箱: 修改

重新发送激活邮件 进入我的邮箱