调用定义到正在运行的图中的方法

时间:2017-07-12 15:57:00

标签: matlab class properties function-handle matlab-app-designer

我有以下UIFigure

classdef gui < matlab.apps.AppBase
   ...
   function app = gui
      % Construct app
   end
   ...
   properties (Access = public)
      myFuncRef = @myFun
   end
   ...
   function myFun(app)
      % do something
   end
   ...
end

我在其中定义了方法myFun

如果数字正在运行(也就是说,它显示了一个窗口),我如何从MATLAB的命令窗口调用方法myFun?我试过

h = findobj(0, 'type', 'figure');
funcRef = get(h, 'myFuncRef');
funcRef(h);

但我收到了错误

  

运行模拟和模拟时发生错误   终止引起:功能&#39; subsindex&#39;未定义值   class&#39; matlab.graphics.GraphicsPlaceholder&#39;。

提前致谢!

2 个答案:

答案 0 :(得分:0)

试试这个:

h = findobj(gcf,'-method','myFuncRef') 

h = findobj(0,'class','gui') 

让我知道它是否有效

问题可能是你得到了findobj(0, 'type', 'figure')的数字,这只是一个由App-Class模仿的Grahics Obejct。

答案 1 :(得分:0)

首先,我想解决您遇到的错误。原因是您拨打signature时返回的h为空。相反,您应该使用findobj() [src]

我知道当你引用的方法是findall(0,'Type','Figure',...)时,这是可能的。考虑以下课程:

static

然后,运行以下内容将产生所需的结果:

classdef q45062561 < matlab.apps.AppBase

   properties (Access = public)
      myFuncRef = @q45062561.myFun
   end

   methods (Access = private, Static = true)
     function myFun()
        disp('This works!')
     end
   end

end

备注:

  1. 我不是通过>> F = q45062561; >> F.myFuncRef() This works! 找到数字的句柄,而只是在创作过程中存储它。
  2. findobj的修饰符从问题中不清楚,因此我无法知道此解决方案是否适用于您的情况。
  3. 就个人而言,我认为最好定义方法myFun和/或public,而不是使用存储在属性中的函数引用。