How can I get DataCursor information from stackedPlot on ButtonDown callback function?

23 views (last 30 days)
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

Accepted Answer

Adam Danz
Adam Danz on 18 Feb 2021
Edited: Adam Danz on 18 Feb 2021
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 Comments
julien gilles
julien gilles on 19 Feb 2021
Thanks, but I am not interested in printed values.
What I really want is to recover the x value.
Then I find into my StackedLineChart.SourceTable the only line where x value appears (time column).
When I have got it, I write into a csv file my StackedLineChart.SourceTable line with all values. Because I am not showing all values into my Stackedplot, in fact I reduce the visible values of my stackedPlot with stackedPlot option.
Adam Danz
Adam Danz on 19 Feb 2021
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.

Sign in to comment.

More Answers (0)

Categories

Find more on Line Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!