Retrieving data from a callback

6 views (last 30 days)
Harold
Harold on 6 Feb 2013
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.

Accepted Answer

Harold
Harold on 9 Feb 2013
Edited: Harold on 9 Feb 2013
Here are the zipped up files in anyone is interested in this problem.
Edit: So I was messing around with this program some more today and realized that my data is getting passed to the main GUI. It's just that the data does not get updated in Backprop_GUI_OpeningFcn. It only shows data = []. I typed in handles.data in the weight_slider_val_Callback and I get the data values if I move the slider. Question is this, why can't I see these values in the Backprop_GUI_OpeningFcn? This handle problem ties in with the weight_slider_val handle because if I comment call to @mouse_over, I can operate the slider with no problem.
  1 Comment
Harold
Harold on 10 Feb 2013
I figured out the problem with the slider. What I did was also pass handles into mouse_over. So I changed the set command to this in the main GUI...
set (gcf, 'WindowButtonMotionFcn', {@mouse_over,handles});
and the following in the mouse_over function
set(gca,'ButtonDownFcn', {@select_point,handles});

Sign in to comment.

More Answers (3)

Walter Roberson
Walter Roberson on 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.
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 Comment
Harold
Harold on 6 Feb 2013
I have made the suggested additions but I still get the same result. I'll mess with it some more tomorrow. If I can't get anywhere I'll have upload this and post the link here.

Sign in to comment.


Harold
Harold on 7 Feb 2013
I have tried to use
guidata(gcf, handles);
I used this in the function where the variable was getting change select_point and made sure to set the variable data as global in the main GUI and also in select_point. When I run the program, the error "Not enough input arguments" points to the line where I use
guidata(gcf, handles);
I'm not sure what the problem and I really don't know what else to try.
  4 Comments
Walter Roberson
Walter Roberson on 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
Harold on 8 Feb 2013
Edited: Harold on 8 Feb 2013
I'm using R2012a. I may have interpreted the results wrong since I am not an expert with GUIDE. When I run the program, the following shows up in the workspace.
btn_type = normal
data = [1.8429,0.4046]
eventdata = []
hObject 6.0049
ma = mouse position in current axes
n = 1
The problem is within the select_point function. The path is as follows
1)Main GUI
2) @mouse_over using set (gcf, 'WindowButtonMotionFcn', @mouse_over)
3) @select_point using set(gca,'ButtonDownFcn', @select_point) if cursor is within plot bounds.
Here is the function line
function [] = select_point(hObject, eventdata, handles)
Here is a link to the files for this program. Maybe this will better explain my code.

Sign in to comment.


Harold
Harold on 9 Feb 2013
Still no luck with this. In the function select_points I included the following.
handles.data = data;
.
.
.
.
.
guidata(gcf, handles);
I then included the same for mouse_over. The problem with doing this now is that the slider does not update the value shown in the text box. I get this error
Reference to non-existent field 'weight_slider_val'.
I have no idea why MATLAB is being so difficult with passing a variable back out of a function. Is it because I call it using the @ symbol? I have a lot of programs that would really benefit from this mouseover/select point program.
  1 Comment
Walter Roberson
Walter Roberson on 9 Feb 2013
Package your files in a .zip instead of a .rar and someone might take a look at them.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!