Retrieving data from a callback
Ältere Kommentare anzeigen
I need to get data from within a function called by the main GUI. It's a function that gets called when I move my mouse over a plot. n is a counting variable for the row number in the data matrix. Every time the left mouse clicks, n increases by one.
Within the main GUI I have
global data n
%initialize data
data = [];
n = 1;
handles.data = data;
handles.n = n;
set (gcf, 'WindowButtonMotionFcn', @mouseover);
Within mouseover I call another function
set(gca,'ButtonDownFcn', @select_point);
Within select_point, the coordinates of the mouse click are stored in the variable data. In the function mouseover, I can see what values are stored in data. However, in the main GUI I only get an empty matrix for data. I also don't get an updated value for n.
Akzeptierte Antwort
Weitere Antworten (3)
Walter Roberson
am 6 Feb. 2013
In the main gui, you need to use
handles = guidata(gcf);
each time that you want to look for updates to handles.data or handles.n .
Also, in each routine that you update either of those, you need to have
guidata(gcf, handles);
after you update the object.
See also http://matlab.wikia.com/wiki/FAQ#How_can_I_share_data_between_callback_functions_in_my_GUI.3F
Note: if you are changing the global variables data or n in your routines, then be sure that you declare the variables as "global" in the routines that change them and the routines that need to look at the updated values.
1 Kommentar
Harold
am 6 Feb. 2013
Harold
am 7 Feb. 2013
4 Kommentare
Walter Roberson
am 7 Feb. 2013
Please put in a breakpoint at that line and run until the breakpoint is hit. When it is hit, please show the results of
which gcf
which handles
whos gcf
whos handles
'gcf called is', gcf()
'handles called is', handles()
Walter Roberson
am 8 Feb. 2013
You indicate that whos gcf returned a structure. It isn't supposed to do that unless you have assigned a structure to "gcf", overriding its use as a function. But then gcf() would not return a handle like it does. Which MATLAB version are you using? And please check to see whether you ever assign anything to gcf .
I am puzzled at present as to how "which handles" can say it is not found, and "whos handles" shows nothing, and yet handles can be interpreted as a function.
The place you are encountering this problem: is it within a function? If so then please show the "function" line for the function.
Harold
am 9 Feb. 2013
1 Kommentar
Walter Roberson
am 9 Feb. 2013
Package your files in a .zip instead of a .rar and someone might take a look at them.
Kategorien
Mehr zu Creating, Deleting, and Querying Graphics Objects finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!