callback function handle syntax inside objects?

1 次查看(过去 30 天)
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.
Example:
cg=CCUCGUI('10.0.1.1');
cg.navi('up');
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.
set(obj.guiobj.buttonup,'Callback','obj.navi(''up'');');
%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});
set(obj.guiobj.buttonup,'Callback'{@obj.navi,'up'});
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

回答(1 个)

Sean
Sean 2012-7-12
编辑:Sean 2012-7-12
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:
cvh=cellstr(char(com.mathworks.mlservices.MLCommandHistoryServices.getSessionHistory));
objname=strtok(char(cvh(end)),'=');
clear cvh
2) Use the output object instance name in setting the callback:
set(obj.guiobj.buttonup,'Callback',strcat(objname,'.navi(''up'');'));
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. :)

类别

Help CenterFile Exchange 中查找有关 Environment and Settings 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by