Can't get windowButtonMotionFcn to work

This is my first time working on matlab gui and to be honest I don't know what I'm doing. Hopefully I can provide enough information to solve this. Below are the relevant parts of my program.
classdef title < handle
% code
methods
function obj = title(dirname)
% code
figH = figure(...
'WindowButtonMotionFcn' , @mouse);
end
%
function mouse(obj, hObj, varargin);
obj_han = get(hObj, 'currentpoint');
set(obj.handles.cursorX, 'String', num2str(obj_han(1)));
set(obj.handles.cursorY, 'String', num2str(obj_han(2)));
end
end
"Undefined function 'mouse' for input arguments of type 'matlab.ui.Figure'. Error while evaluating Figure WindowButtonMotionFcn"
If I leave the code of 'mouse' unchanged but change the function to:
function mouse(hObj, varargin);
and define it within function obj = title(dirname), then it works until a call to mouseButtonDwn (which executes changing WindowButtonMotionFcn to a different function, then resets WindowButtonMotionFcn to @mouse). After this the error is the same as the above.
What can I do here?

2 Commenti

You need to show some code, there is too little information here to understand what the setup is.
Thanks for pointing this out, I've edited the question and provided some code. Hopefully this makes it clearer :)

Accedi per commentare.

 Risposta accettata

Adam
Adam il 9 Giu 2016
You need to use @obj.mouse in your call to create the figure because the function is part of your class and so needs to have the object scope.
After that I assume 'handles' exists as a structure in your class. When I run the small snippet above with the corrected syntax obviously I get an error that 'handles' does not exist, but I imagine that is just because you have only included the bits of your class relevant to the problem.

3 Commenti

Cheers! I worked it out earlier today much to my relief, my eyes were simply glazing over it. It certainly didn't take you as long to realise :D as it did for me.
One thing I meant to add, but forgot. Don't name a class 'title', you will cause the inbuilt function of the same name to be inaccessible as it will shadow your class on the path which is never a good idea. If you ever want to plot something with the normal title function you will run into errors.
I regularly run e.g.
which title
to check before naming something whether I am about to clash with a built-in function.
That's a great tip, I'll definitely use that when naming functions in the future!

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Creating, Deleting, and Querying Graphics Objects in Centro assistenza e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by