callback function handle syntax inside objects?

1 view (last 30 days)
Sean on 12 Jul 2012
I have an object that generates a gui and controls some external equipment. I am trying to set things up so that a particular button press on the gui sends a particular command to the external equipment (without success). The command is sent using one of the object methods.
The above code works, but my numerous attempts to create a callback inside the object contructor have failed miserably.
I have tried the following and more, but my understanding of callback function handle syntax is clearly insufficient to the task.
%Note the above executes, but 'obj' does not exist in main MATLAB workspace
set(obj.guiobj.buttonup,'Callback',@() obj.navi('up'));
set(obj.guiobj.buttonup,'Callback',{@() obj.navi('up')});
set(obj.guiobj.buttonup,'Callback',{@('up') obj.navi});
tmp='up';set(obj.guiobj.buttonup,'Callback',@(tmp) obj.navi);
tmp='up';set(obj.guiobj.buttonup,'Callback',{@(tmp), obj.navi});
Any help would be appreciated, Thanks, Sean

Answers (1)

Sean on 12 Jul 2012
Edited: Sean on 12 Jul 2012
I have found a way to accomplish what I was trying to accomplish, although it isn't by using function handles. In case anyone else is trying to do something similar, here is the method I used:
1) Get the output object instance name as soon as the constructor starts running by:
clear cvh
2) Use the output object instance name in setting the callback:
Since string based callback assignment executes in the base workspace, this directly simulates the equivalent command line action (and thus works).
If anyone can answer the question as asked (i.e. about function handles), I am still interested for future reference though. :)

Community Treasure Hunt

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

Start Hunting!