4

I am trying to compare the click-obtained coordinates with a loaded matrix MT.

I wrote a "loadMT" function to load the matrix MT, stored it into handles, and when it's finished it gives this(which I believe is a sign that MT has been stored in handles)

        figure1: 173.0090
          y_lbl: 11.0092
lbl_last_action: 10.0092
 AverageModulus: 9.0092
          axes1: 4.0092
         slider: 3.0092
         LoadMT: 0.0092
      LoadImage: 174.0090
         output: 173.0090
      frameName: [599x1 struct]
       pathname: [1x43 char]
       no_frame: 599
             MT: [4318x7 double]
   currentframe: 101

Mouse click coordinate was obtained in a way recommended by this post MATLAB how to get mouse click coordinates. The core code is

imageHandle = imshow(imObj);
set(imageHandle,'ButtonDownFcn',@ImageClickCallback);

function ImageClickCallback ( objectHandle , eventData )
MT=handles.MT;
axesHandle  = get(objectHandle,'Parent');
coordinates = get(axesHandle,'CurrentPoint'); 
coordinates = coordinates(1,1:2);

And it has worked and I have got the coordinate.(without the MT=handles.MT line)

However,when I click the image, it gives this error, means I failed to extract MT from the handles

Undefined variable "handles" or class "handles.MT".

Error in GUI>ImageClickCallback (line 159)
MT=handles.MT;

How may I pass the MT from handles to a local variables here? My searching results really confuse me. Thanks in advance

Community
  • 1
  • 1
Suicide Bunny
  • 894
  • 1
  • 10
  • 23

1 Answers1

4

Try this

set(imageHandle,'ButtonDownFcn',{@ImageClickCallback,handles};

Also in your function ImageClickCallback you need to add a category for handles as so:

function ImageClickCallback(objectHandle, eventData, handles)

DreamBig
  • 134
  • 1
  • 1
  • 8
  • Thanks for the suggestion. It still gives the same error:((i put that under the slider_callback function, which i use to change the frames, after each click the handles it gives include the MT as above for the loadMT function) – Suicide Bunny Nov 20 '13 at 15:59
  • after I add the handles input the error becomes "Error using GUI>ImageClickCallback (line 160) Not enough input arguments." – Suicide Bunny Nov 20 '13 at 16:01
  • But then it won't respond to my mouse click, will it? – Suicide Bunny Nov 20 '13 at 16:04
  • The mouse click is currently set to the figure. If the slider is what you are clicking you should try to set it through the slider callback. – DreamBig Nov 20 '13 at 16:10
  • It is set in the slider call back. I am sorry I didn't make it clear. – Suicide Bunny Nov 20 '13 at 16:11
  • So what are you trying to do with a slider then? Please paste your code for the slider (maybe edit the post). – DreamBig Nov 20 '13 at 16:15
  • I am using the slider to go to next frame(reload a new image). I found a post here http://www.mathworks.com/matlabcentral/newsreader/view_thread/290614 and have solved the problem by adding a line handles=guidata(objectHandle); before MT=handles.MT in the imageClickCallback function. Thanks very much for your help. The discussion helps me formulated my question more specificly and have helped me refined the searching and find the solution. – Suicide Bunny Nov 20 '13 at 16:19
  • 3
    So what you did there was calling something similar to this `guidata(hObject, handles);` That updates the handles through the GUI which should be called each time you create something that you want to store in the handles structure and then you should pass it through like in my post. In your case you just basically set a reference to the handles by calling `handles = guidata(objectHandle)` which is valid passing data through a gui. [See guidata here](http://www.mathworks.com/help/matlab/creating_guis/ways-to-manage-data-in-a-guide-gui.html#f5-998711) – DreamBig Nov 20 '13 at 16:29