Passing handles to datatip user function in GUI

Hello,
I'd like to pass data to a user defined fucntion that is run every time that the data tip is used in order that I can display further information about the data on the graph interactively.
The code in the main GUI looks like:
cursorMode = datacursormode(gcf);
set(handles.cursorMode, 'enable','on', 'UpdateFcn', @setDataTipTxtFFT)
This calls:
function output_txt = setDataTipTxtFFT(obj,event_obj)
pos = get(event_obj,'Position');
line_id = get(event_obj,'Target')
Freq = pos(1)
Amp = pos(2)
s1 = sprintf('freq = %1.4g Telsa', Freq);% = %1.4g T', Freq);
s2 = sprintf('amp = %1.4g', Amp);% = %1.4g bar', Amp);
%s3 = sprintf('amp = %1.4g', P);% = %1.4g bar', Amp);
output_txt = {s1; s2; s3};
-----------------------------------------------------------
What I would like to do is cross reference the index of the various plots with the index of the loaded files which are stored in the main gui's handle structure.
I don't seem to be able to just call the handles.LoadedFileIndex or such like inside the function @setDataTipTxtFFT
I tried passing handles in the variable list of @setDataTipTxtFFT
Should this work, and if not, could you suggest what the most eficient way of doing this might be. I though the handles structure was meant to get around the defining of global variables, which I could access with setappdata and getappdata
Iss this one of those instances where using this method is unavoidable?
Thanks!

 Accepted Answer

After the "function" line, add
handles = guidata( ancestor(obj, 'Figure') );
Note: this will not work if the figure with the plot whose datatips you are using is not the same as your figure GUI. If that is the case, indicate that and I will show the more complicated approach.

5 Comments

Thanks for your response!
I'm afraid that this doesn't seem to work. I tried the line you suggested as well as inserting the name of the tag of the figure in where you have the 'Figure'
It also doesn't throw up any error so I am not sure what is going on.
I have two axes on the figure not two separate guis - the function below doesn't show the second disp line, but it does do the 1st one if I click the mouse so it just seems to hang at first disp ()
function output_txt = setDataTipTxtFFT(obj,event_obj)
disp('in here at least1')
handles = guidata(ancestor(obj, 'Analysis_GUI_V2') );
pos = get(event_obj,'Position');
line_id = get(event_obj,'Target')
disp('in here at least2')
Freq = pos(1)
You should not use the tag 'Analysis_GUI_V2': the word 'figure' refers to the Type field of the handle you are looking for.
Change it back to 'figure' and then put a breakpoint in at that line. Run, and when it stops, command
disp(get(obj, 'Type'))
ancestor(obj, 'figure')
and see if that returns.
Thanks, I tried this and got:
K>> disp(get(obj, 'Type'))
K>> ancestor(obj, 'figure')
ans =
[]
Any ideas why this should be empty?
Odd, but it is documented. Okay, so looks like it should be
handles = guidata( ancestor(event_obj.Target, 'figure') );
Thanks, that was it!

Sign in to comment.

More Answers (0)

Categories

Find more on Creating, Deleting, and Querying Graphics Objects 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!