
How can I get DataCursor information from stackedPlot on ButtonDown callback function?
21 次查看(过去 30 天)
显示 更早的评论
Hi,
I am using StackedPlot in order to represent a set of values. What is interesting with the stackedPlot, is giving you with dataCursor over a location to display y-values for each plot.
My goal is to recover theese information (x-value and y-yvalues) by a double click interaction with the figure that containing my StackedPlot
I am able to use callback function windowButtonDown and to know if I am into my stackedPlot. My problem is that I am not able to obtain position and datacursor y-values information. Can you help me with this ?
Many thanks
Julien
0 个评论
采纳的回答
Adam Danz
2021-2-18
编辑:Adam Danz
2021-2-18
This solution uses undocumented methods of obtaining the data tip lables (see inline comments). As with all undocumented approaches, things can change in future releases. When the user clicks on a stackedplot the datacursor values and the x value will appear in the command window along with the associated axis labels. You can easily print the value to a file instead. Note that callback functions do not return outputs.
See footnotes for important details.
% Create statckedplot
fig = figure();
x = linspace(-5*pi, 5*pi, 500)';
y = [sin(2*x), sin(x), sin(.5*x), sin(.2*x), -sin(.1*x)];
sph = stackedplot(y,'XData',x,'DisplayLabels',["A","B","C","D","E"]);
% Assign callback function when user clicks on figure
fig.WindowButtonDownFcn = {@myFunc, sph};
function myFunc(~,~,sph)
% sph is the stackedplot handle.
pause(0.5) % See footnote [1]
% Avoid seeing warning associated with undocumented methods
origState = warning('query', 'MATLAB:structOnObject');
cleanup = onCleanup(@()warning(origState));
warning('off','MATLAB:structOnObject')
S = struct(sph); % Undocumented
Sdc = struct(S.DataCursor); % Undocumented
% Get data tip values
dataTipLabels = Sdc.DatatipLabels;
dataTipStr = get([dataTipLabels{:}],'String');
axLabels = sph.DisplayLabels;
% If the vertical CursorLine is not visible (ie, the mouse is not in the axes) or
% if the data tips are all NaN (sometimes this happens on the first click only)
% then do nothing. Otherwise, print the data tip values and axis labels to command
% window.
if strcmpi(Sdc.CursorLine.Visible,'off') || all(cellfun('isempty',dataTipStr(1:end-1)))
return
else
yDataStr = compose(' %s = %.3g', string(axLabels), string(dataTipStr(1:end-1)));
fprintf('\nAt x = %s\n%s\n',dataTipStr{end},strjoin(yDataStr,newline))
end
end
%% Footnotes
% [1] The pause(0.5) is needed to give time for the data tip values to
% update. Notice that the line must remain still for a brief period
% before the datatips appear. In that time, the datatip values from
% the previous datatip appearance are stored and we must wait for them
% to regenerate before returning the updated value. This lag is a
% property of the stackedplot data cursor: Sdc.LingerTime.

4 个评论
Adam Danz
2021-2-19
I see. So you're retrieving the x value, finding its index in your input x-data (with tolerance if it's floating pt decimal), and then using the index value to retrieve the y-values from the input table stored in the stacked line obj. Sounds like a good plan.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Interactive Control and Callbacks 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!