matlab different datacursormode for each subplot

8 次查看(过去 30 天)
I want to plot 2 figures by using subplot
subplot 211 subplot 212
but I'd like to use different cursormode each subplot211 and subplot 212
for example For subplot 211, If I pick datacursormode, then data(X:-,Y:-,text:-) would be shown like this.
Theother subplot 212, If I pick datacursor, then the other data(X:-, K:-) should be shown.
I have no idea how to control each subplot using datacursormde
Please let me know good tip to solve this problem.

回答(3 个)

Walter Roberson
Walter Roberson 2016-11-25
Unfortunately datacursormode is a figure property. Your code would have to figure out which subplot the cursor was over and change the output string as appropriate.

Jasper van Casteren
Jasper van Casteren 2018-11-22
I use
fig = figure;
ax1 = subplot(whatever);
ax2 = subplot(whatever);
datacursor = datacursormode(fig);
datacursor.set('UpdateFcn',@MyFunc);
% MyFunc must be an embedded function to know ax1 and ax2
function MyFunc(Target, evtObj)
if Target.Parent==ax1
whatever;
else
whatever;
end
end

Spencer Rhodes
Spencer Rhodes 2019-6-18
Another way to do it:
set(datacursormode(gcf),'UpdateFcn',@(~,evt) timeheight_cursor_update(evt,AH1))
where AH1 is the axis handle of the subplot I want the cursor update function to be used in.
My timeheight_cursor_update function is then this:
function [txt] = timeheight_cursor_update(evt,varargin)
%TIMEHEIGHT_CURSOR_UPDATE Useful when wanting to inspect a time-height plot
%and see the actual dates/times instead of just the Matlab datenums.
%
% INPUTS
% evt: The event struct for the cursor click which holds the Target
% and Position.
% varargin: If using subplots, can provide the one axis you want this
% update function to apply to. If you click the cursor in
% any other subplot, it won't return.
if length(varargin) == 1
% check if looking at provided axis
if varargin{1} == evt.Target
% good to go
elseif isobject(varargin{1}) ...
&& ismember('Children',properties(varargin{1})) ...
&& varargin{1}.Children == evt.Target
% also good to go
else
return; % cursor must be in a different axis, use default txt
end
end
pos = get(evt, 'Position');
txt = {['Time: ' datestr(pos(1))], ...
sprintf('Height: %.2f', pos(2))};
end
I'm not sure how to return Matlab's default tooltip, still looking into that. But at least this is a bit more elegant than what Jasper suggested, and you don't need a nested function, so it works in a script.
Best,
Spencer

类别

Help CenterFile Exchange 中查找有关 Title 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by