如何在数据光标模式下更改双 y 轴和单 x 轴图的输出文本
How to change output text in datacursor mode for a plot with double y-axis with single x-axis
我有以下代码:
datee = {'23-10-201511:36:24', '23-10-201511:37:24', '23-10-201511:38:24', '23-10-201511:39:24', '23-10-201511:40:24', '23-10-201511:41:24', '23-10-201511:42:24', '23-10-201511:43:24', '23-10-201511:44:24', '23-10-201511:45:24'};
Temperature = [23.6, 23.6, 23.7, 23.7, 23.7, 23.7, 23.7, 23.7, 23.7, 23.7];
Humidity = [40, 40, 40, 39.9, 39.8, 39.7, 39.8, 39.8, 39.8, 39.8];
C = datenum(datee,'dd-mm-yyyyHH:MM:SS');
[ax,h1,h2] = plotyy(C,Temperature,C,Humidity)
hold on;
datestr(C);
datetick(ax(1),'x','HH:MM','keepticks');
datetick(ax(2),'x','HH:MM','keepticks');
xlabel('time [hours]');
ylabel(ax(1),'Temperature [°C]');
ylabel(ax(2),'Humidity [%rH]');
dcm_obj = datacursormode(gcf);
set(dcm_obj,'UpdateFcn',@myfunction);
function [output_txt] = myfunction(~,event_obj)
% Display the position of the data cursor
% obj Currently not used (empty)
% event_obj Handle to event object
% output_txt Data cursor text string (string or cell array of strings).
pos = get(event_obj,'Position');
output_txt= {['time: ', datestr(pos(1))],...
['Temperature: ',num2str(pos(2),4)]};
output_txt= {['time: ', datestr(pos(1))],...
['Humidity: ',num2str(pos(2),4)]};
If there is a Z-coordinate in the position, display it as well
if length(pos) > 2
output_txt{end+1} = ['Z: ',num2str(pos(3),4)];
end
如果我使用此代码,我只会看到两个 y 轴的 time
和 temperature
变量,但看不到湿度变量。
我想看到类似的东西,如果我点击一个 y 轴,它必须显示 time
和 temperature
,对于其他 y 轴,time
和 humidity
.
如果我对您的问题的解释正确,您希望根据所点击的行获得不同的数据提示。完成此操作的一种方法是将 event_obj
的 Target
与 plotyy
:
返回的行对象的句柄进行比较
function testcode
datee = {'23-10-201511:36:24', '23-10-201511:37:24', '23-10-201511:38:24', '23-10-201511:39:24', '23-10-201511:40:24', '23-10-201511:41:24', '23-10-201511:42:24', '23-10-201511:43:24', '23-10-201511:44:24', '23-10-201511:45:24'};
Temperature = [23.6, 23.6, 23.7, 23.7, 23.7, 23.7, 23.7, 23.7, 23.7, 23.7];
Humidity = [40, 40, 40, 39.9, 39.8, 39.7, 39.8, 39.8, 39.8, 39.8];
C = datenum(datee,'dd-mm-yyyyHH:MM:SS');
[ax,h1,h2] = plotyy(C,Temperature,C,Humidity);
hold on;
datestr(C);
datetick(ax(1),'x','HH:MM','keepticks');
datetick(ax(2),'x','HH:MM','keepticks');
xlabel('time [hours]');
ylabel(ax(1),'Temperature [°C]');
ylabel(ax(2),'Humidity [%rH]');
dcm_obj = datacursormode(gcf);
set(dcm_obj,'UpdateFcn',{@myfunction, [h1, h2]});
end
function [output_txt] = myfunction(~,event_obj, linehandles)
% Display the position of the data cursor
% obj Currently not used (empty)
% event_obj Handle to event object
% output_txt Data cursor text string (string or cell array of strings).
pos = get(event_obj,'Position');
targetline = get(event_obj, 'Target');
switch targetline
case linehandles(1)
output_txt = {['Time: ', datestr(pos(1))], ...
['Temperature: ',num2str(pos(2),4)] ...
};
case linehandles(2)
output_txt = {['Time: ', datestr(pos(1))], ...
['Humidity: ',num2str(pos(2),4)] ...
};
otherwise
% No match
output_txt = {['X: ', datestr(pos(1))], ...
['Y: ',num2str(pos(2),4)] ...
};
end
% If there is a Z-coordinate in the position, display it as well
if length(pos) > 2
output_txt{end+1} = ['Z: ',num2str(pos(3),4)];
end
end
此处有两个主要变化:我已将 h1
和 h2
传递给您的回调以与 case-switch
statement I added. You can use an equivalent if-else
语句一起使用,但我个人认为 case-switch
更优雅。
以及相应的数据提示:
我有以下代码:
datee = {'23-10-201511:36:24', '23-10-201511:37:24', '23-10-201511:38:24', '23-10-201511:39:24', '23-10-201511:40:24', '23-10-201511:41:24', '23-10-201511:42:24', '23-10-201511:43:24', '23-10-201511:44:24', '23-10-201511:45:24'};
Temperature = [23.6, 23.6, 23.7, 23.7, 23.7, 23.7, 23.7, 23.7, 23.7, 23.7];
Humidity = [40, 40, 40, 39.9, 39.8, 39.7, 39.8, 39.8, 39.8, 39.8];
C = datenum(datee,'dd-mm-yyyyHH:MM:SS');
[ax,h1,h2] = plotyy(C,Temperature,C,Humidity)
hold on;
datestr(C);
datetick(ax(1),'x','HH:MM','keepticks');
datetick(ax(2),'x','HH:MM','keepticks');
xlabel('time [hours]');
ylabel(ax(1),'Temperature [°C]');
ylabel(ax(2),'Humidity [%rH]');
dcm_obj = datacursormode(gcf);
set(dcm_obj,'UpdateFcn',@myfunction);
function [output_txt] = myfunction(~,event_obj)
% Display the position of the data cursor
% obj Currently not used (empty)
% event_obj Handle to event object
% output_txt Data cursor text string (string or cell array of strings).
pos = get(event_obj,'Position');
output_txt= {['time: ', datestr(pos(1))],...
['Temperature: ',num2str(pos(2),4)]};
output_txt= {['time: ', datestr(pos(1))],...
['Humidity: ',num2str(pos(2),4)]};
If there is a Z-coordinate in the position, display it as well
if length(pos) > 2
output_txt{end+1} = ['Z: ',num2str(pos(3),4)];
end
如果我使用此代码,我只会看到两个 y 轴的 time
和 temperature
变量,但看不到湿度变量。
我想看到类似的东西,如果我点击一个 y 轴,它必须显示 time
和 temperature
,对于其他 y 轴,time
和 humidity
.
如果我对您的问题的解释正确,您希望根据所点击的行获得不同的数据提示。完成此操作的一种方法是将 event_obj
的 Target
与 plotyy
:
function testcode
datee = {'23-10-201511:36:24', '23-10-201511:37:24', '23-10-201511:38:24', '23-10-201511:39:24', '23-10-201511:40:24', '23-10-201511:41:24', '23-10-201511:42:24', '23-10-201511:43:24', '23-10-201511:44:24', '23-10-201511:45:24'};
Temperature = [23.6, 23.6, 23.7, 23.7, 23.7, 23.7, 23.7, 23.7, 23.7, 23.7];
Humidity = [40, 40, 40, 39.9, 39.8, 39.7, 39.8, 39.8, 39.8, 39.8];
C = datenum(datee,'dd-mm-yyyyHH:MM:SS');
[ax,h1,h2] = plotyy(C,Temperature,C,Humidity);
hold on;
datestr(C);
datetick(ax(1),'x','HH:MM','keepticks');
datetick(ax(2),'x','HH:MM','keepticks');
xlabel('time [hours]');
ylabel(ax(1),'Temperature [°C]');
ylabel(ax(2),'Humidity [%rH]');
dcm_obj = datacursormode(gcf);
set(dcm_obj,'UpdateFcn',{@myfunction, [h1, h2]});
end
function [output_txt] = myfunction(~,event_obj, linehandles)
% Display the position of the data cursor
% obj Currently not used (empty)
% event_obj Handle to event object
% output_txt Data cursor text string (string or cell array of strings).
pos = get(event_obj,'Position');
targetline = get(event_obj, 'Target');
switch targetline
case linehandles(1)
output_txt = {['Time: ', datestr(pos(1))], ...
['Temperature: ',num2str(pos(2),4)] ...
};
case linehandles(2)
output_txt = {['Time: ', datestr(pos(1))], ...
['Humidity: ',num2str(pos(2),4)] ...
};
otherwise
% No match
output_txt = {['X: ', datestr(pos(1))], ...
['Y: ',num2str(pos(2),4)] ...
};
end
% If there is a Z-coordinate in the position, display it as well
if length(pos) > 2
output_txt{end+1} = ['Z: ',num2str(pos(3),4)];
end
end
此处有两个主要变化:我已将 h1
和 h2
传递给您的回调以与 case-switch
statement I added. You can use an equivalent if-else
语句一起使用,但我个人认为 case-switch
更优雅。
以及相应的数据提示: