Matlab GUI select 要绘制的轴
Matlab GUI select which axes to plot
我正在使用下面的代码绘制来自串口的数据。由于我有两个绘图轴,我如何 select 这个绘图的特定轴?
从类似的问题中,我发现他们使用轴(handles.axes2);。因为我在程序开始时声明了情节,我应该把这行代码放在哪里?我尝试在指定情节标题等之前放置它,但它不起作用。
% Serial Data Logger
% Yu Hin Hau
% 7/9/2013
% **CLOSE PLOT TO END SESSION
clear
clc
%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',...
'LineWidth',1,...
'MarkerEdgeColor','k',...
'MarkerFaceColor',[.49 1 .63],...
'MarkerSize',2);
title(plotTitle,'FontSize',25);
xlabel(xLabel,'FontSize',15);
ylabel(yLabel,'FontSize',15);
axis([0 10 min max]);
grid(plotGrid);
%Open Serial COM Port
s = serial(serialPort)
disp('Close Plot to End Session');
fopen(s);
tic
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]);
else
set(plotGraph,'XData',time,'YData',data);
axis([0 time(count) min max]);
end
%Allow MATLAB to Update Plot
pause(delay);
end
end
%Close Serial COM Port and Delete useless Variables
fclose(s);
clear count dat delay max min plotGraph plotGrid plotTitle s ...
scrollWidth serialPort xLabel yLabel;
disp('Session Terminated...');
获得可靠绘图和操作的诀窍是 在创建 plot
或任何其他图形对象时始终使用 Parent
parameter 显式指定父级 .所有图形对象都支持此参数。
hax = axes();
plot(x,y, 'Parent', hax);
另一种选择,如 所建议,是将父轴指定为 plot
的第一个输入:
plot(hax, x, y);
我个人更喜欢使用 Parent
参数作为参数值对,因为这种行为在 所有 图形对象中都是一致的。
在使用其他操作轴的函数时,您还应该指定轴句柄。
xlabel(hax, 'XLabel')
ylabel(hax, 'YLabel')
title(hax, 'This is a title')
axis(hax, [0 0 1 1])
grid(hax, 'on')
hold(hax, 'on')
如果您正在处理交互式 GUI,这一点尤为重要,因为用户可以轻松地单击绘图中间的不同轴,从而导致 gca
的值意外更改。此外,更改当前轴(使用 axes(hax)
)可能会导致糟糕的用户体验。
总结
对于您的特定代码,这将涉及更改您的初始 plot
调用:
plotGraph = plot(time,data,'-mo',...
'LineWidth',1,...
'MarkerEdgeColor','k',...
'MarkerFaceColor',[.49 1 .63],...
'MarkerSize',2, ...
'Parent', handles.axes2);
我还建议在您对 grid
、title
、axis
、xlabel
和 ylabel
的调用中添加显式轴句柄,以确保他们的目标是你想要的轴。
我正在使用下面的代码绘制来自串口的数据。由于我有两个绘图轴,我如何 select 这个绘图的特定轴? 从类似的问题中,我发现他们使用轴(handles.axes2);。因为我在程序开始时声明了情节,我应该把这行代码放在哪里?我尝试在指定情节标题等之前放置它,但它不起作用。
% Serial Data Logger
% Yu Hin Hau
% 7/9/2013
% **CLOSE PLOT TO END SESSION
clear
clc
%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',...
'LineWidth',1,...
'MarkerEdgeColor','k',...
'MarkerFaceColor',[.49 1 .63],...
'MarkerSize',2);
title(plotTitle,'FontSize',25);
xlabel(xLabel,'FontSize',15);
ylabel(yLabel,'FontSize',15);
axis([0 10 min max]);
grid(plotGrid);
%Open Serial COM Port
s = serial(serialPort)
disp('Close Plot to End Session');
fopen(s);
tic
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]);
else
set(plotGraph,'XData',time,'YData',data);
axis([0 time(count) min max]);
end
%Allow MATLAB to Update Plot
pause(delay);
end
end
%Close Serial COM Port and Delete useless Variables
fclose(s);
clear count dat delay max min plotGraph plotGrid plotTitle s ...
scrollWidth serialPort xLabel yLabel;
disp('Session Terminated...');
获得可靠绘图和操作的诀窍是 在创建 plot
或任何其他图形对象时始终使用 Parent
parameter 显式指定父级 .所有图形对象都支持此参数。
hax = axes();
plot(x,y, 'Parent', hax);
另一种选择,如 plot
的第一个输入:
plot(hax, x, y);
我个人更喜欢使用 Parent
参数作为参数值对,因为这种行为在 所有 图形对象中都是一致的。
在使用其他操作轴的函数时,您还应该指定轴句柄。
xlabel(hax, 'XLabel')
ylabel(hax, 'YLabel')
title(hax, 'This is a title')
axis(hax, [0 0 1 1])
grid(hax, 'on')
hold(hax, 'on')
如果您正在处理交互式 GUI,这一点尤为重要,因为用户可以轻松地单击绘图中间的不同轴,从而导致 gca
的值意外更改。此外,更改当前轴(使用 axes(hax)
)可能会导致糟糕的用户体验。
总结
对于您的特定代码,这将涉及更改您的初始 plot
调用:
plotGraph = plot(time,data,'-mo',...
'LineWidth',1,...
'MarkerEdgeColor','k',...
'MarkerFaceColor',[.49 1 .63],...
'MarkerSize',2, ...
'Parent', handles.axes2);
我还建议在您对 grid
、title
、axis
、xlabel
和 ylabel
的调用中添加显式轴句柄,以确保他们的目标是你想要的轴。