How to pass the variables to data cursor's updatefcn (datacursormode) in MATLAB GUIDE.

35 views (last 30 days)
Hi All,
I create a data cursor function (daracursormode) in the OpeningFcn().And I want to pass the variables got from "OpeningFcn()" into the "updatefcn()".But the result shows nothing in axes3.It seems like handles.y_fft does not pass into "updatefcn()".Here is the code...
I write an simplified case which can explan my question clearly.(I know the best way in this case is to put handles.y_fft outside "updatefcn" .)
Here is the guide looks like
Am I missing something or making mistake in using it?
Thank you so much!
Chloe

Accepted Answer

Geoff Hayes
Geoff Hayes on 8 Oct 2014
Chloe - note the following three lines of code
dcm_obj = datacursormode(fig);
datacursormode on
set(dcm_obj,'UpdateFcn',@myupdatefcn);
The first line returns the data cursor mode object for the fig figure (so that which shows the sine wave); the second line then enables the data cursor mode for the current figure which is the GUI; and the third line assigns the update function to the figure...which will never fire because the data cursor mode is enabled for the GUI and not the object that dcm_obj refers to.
Also, just prior to updating the handles structure, do the FFT
handles.y_fft = abs(fft(handles.y));
guidata(hObject,handles);
If, like in your attached screen shot, you want the data cursor mode enabled for the GUI, then you could do something like
handles.dcm_obj = datacursormode(hObject);
set(handles.dcm_obj,'Enable','on','UpdateFcn',{@myupdatefcn,hObject});
Note that we will save the dcm_obj as a field to the handles structure. In the second line of code, we enable the data cursor mode for that object, and set its update function. We wrap the update function in braces so that we can pass an additional parameter, hObject, which is the handle to the GUI.
In order to account for the additional parameter, your update function will now look something like
function txt = myupdatefcn(~,event_obj,hFigure)
txt = '';
The problem with enabling the data cursor mode for the figure, is that you have two axes (names axes1 and axes3). This means that this mode will be enabled for both axes, and so the code will have to handle both cases as
% get the axes where the user has clicked
hAxesParent = get(get(event_obj,'Target'),'Parent');
% get the handles structure for the figure/GUI
handles = guidata(hFigure);
% which axes are we in?
if hAxesParent==handles.axes1
pos = get(event_obj,'Position');
txt = {['Time: ',num2str(pos(1))],...
['Amplitude: ',num2str(pos(2))]};
plot(handles.axes3,handles.y_fft);
elseif hAxesParent==handles.axes3
txt = 'what should go here?';
else
% do nothing
end
The above code should update the the other axes. The catch is that the data cursor mode is enabled for your axes3 as well.
  13 Comments
Chloe Cheung
Chloe Cheung on 5 Nov 2014
Dear Geoff,
I really appreciate your help.With your help,I learn lots of GUI in MATLAB. Finally, I have finished my GUI design.It is really fun to play with GUI.And I think I will develop the advanced version of that GUI.
Thank you Geoff!
Best regards,
Chloe

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!