MATLAB Answers

passing variables into custom datacursor in a GUI

14 views (last 30 days)
HpW on 27 Nov 2017
Commented: HpW on 28 Nov 2017
Hello. I have a GUI and I would like to enable a custom datacursor. I have used the following code to setup the dataursor in the GUI to be enabled for all of the axes in the GUI:
dcm = datacursormode;
set(dcm, 'update', {@display_datacursor});
I then have a separate file called display_datacursor.m which contains the following code:
function output_txt = display_datacursor(obj,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).
step = 2;
pos = get(event_obj,'Position');
I = get(event_obj, 'DataIndex');
T = (get(event_obj, 'DataIndex')-1)*step;
output_txt = {['Sample: ',num2str(pos(1),4)],...
['Time (ms): ',num2str((pos(1)-1)*step)],...
['Amplitude (mV): ',num2str(pos(2),4)]};
% If there is a Z-coordinate in the position, display it as well
if length(pos) > 2
output_txt = {['X (mV): ',num2str(pos(1),4)],...
['Y (mV): ',num2str(pos(2),4)],...
['Z (mV): ',num2str(pos(3),4)],...
['Time (ms): ',num2str(T)]};
This allows different data cursor text for the various 2D and 3D graphs in the GUI. This works fine (although it seems to make everything run very slow....if anyone has any idea why this is the case would be appreciated), but I would like to be able to dynamically change the value of 'step'. Ive tried using handles, but it doesnt work. Ive tried passing in arguments like this:
set(dcm, 'update', {@display_datacursor, step});
but this also doesnt work....
How can i pass data into display_datacursor.m?

  1 Comment

Walter Roberson
Walter Roberson on 27 Nov 2017
Minor optimization:
In the step
T = (get(event_obj, 'DataIndex')-1)*step;
you already have get(event_obj, 'DataIndex') stored in I, so you can use
T = (I - 1) * step

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 27 Nov 2017
set(dcm, 'update', {@display_datacursor, step});
function output_txt = display_datacursor(obj, event_obj, step)
and do not assign to step in the function.

  1 Comment

Sign in to comment.

More Answers (0)

Sign in to answer this question.


Translated by