0

所以我有一个从超类继承的类,handle如下所示:

classdef testClass < handle
    properties(Access = private)
        handles_gui;
    end

    methods(Access = public)
        function obj = testClass
            % Preferably like to get inputname here
            obj.handles_gui = obj.init_gui();
        end

        function callback_test(obj,hObject,eventdata)
            disp(inputname(1));
        end
    end

    methods(Access = private)
        function handles_gui = init_gui(obj)            
            handles_gui.figure = figure( ...
                'Tag', 'figure', ...
                'Units', 'characters', ...
                'Position', [50 35 167 25]);    

            handles_gui.button_left = uicontrol( ...
                'Parent', handles_gui.figure, ...
                'Units', 'characters', ...
                'Position', [41 1.2 8 1.8], ...
                'String', 'Test', ...
                'Callback', @(hObject,eventdata) callback_test(obj,hObject,eventdata)); 
        end
    end
end

我想最好在构造函数中获取对象的工作区名称。不确定这是否可行,因为在创建对象之前我不确定是否分配了名称。如果是这样的话,那么我想通过回调来获取它。我有一个 gui,但为了正确传递obj句柄,我必须通过传入函数obj来定义回调。init_gui这意味着当按下按钮时inputname调用when ,它返回,因为它是在回调定义中定义的。但是,如果我通过终端调用,它会返回正确的变量名(结果是有意义的,但这不是我想要的)。一个例子如下所示:callback_test'obj'callback_test

EDU>> test = testClass;
obj (this was called by clicking on the button)
EDU>> test.callback_test
test 
EDU>> 

所以我的问题是:如何获取变量名,最好在构造函数中,如果没有,那么如何通过回调获取它,而不必使用终端。

4

2 回答 2

0

好吧,这只是以防其他人偶然发现这个问题。我解决了它:

classdef testClass < handle
    properties(Access = private)
        handles_gui;
    end

    methods(Access = public)
        function obj = testClass
            % Preferably like to get inputname here
            obj.handles_gui = obj.init_gui();
        end
    end

    methods(Access = private)
        function handles_gui = init_gui(obj)            
            handles_gui.figure = figure( ...
                'Tag', 'figure', ...
                'Units', 'characters', ...
                'Position', [50 35 167 25]);    

            handles_gui.button_left = uicontrol( ...
                'Parent', handles_gui.figure, ...
                'Units', 'characters', ...
                'Position', [41 1.2 8 1.8], ...
                'String', 'Test', ...
                'Callback', @(hObject,eventdata) callback_test(obj,hObject,eventdata)); 
        end

        function callback_test(obj,hObject,eventdata)
            basevars = evalin('base','whos');
            testClassvars = basevars(strcmp({basevars.class},class(obj)));

            found = false;
            for i = 1:length(testClassvars)
                if(eq(evalin('base',testClassvars(i).name),obj))
                    found = true;
                    disp(['Name is: ' testClassvars(i).name]);
                end
            end
            if(~found)
                disp('Handle has been deleted'); 
            end
        end
    end
end

这是我想要的功能;诀窍是用来evalin访问基础工作区中同一类的对象。我以为我需要使用它assignin来执行此操作,但我错了。实现这样的事情是否好是一个不同的问题,但这最终是我想做的,所以它是为其他想做类似事情的人准备的。

输出:

EDU>> a = testClass

a = 

  testClass handle with no properties.
  Methods, Events, Superclasses

EDU>> b = testClass

b = 

  testClass handle with no properties.
  Methods, Events, Superclasses

Name is: b (after clicking on the button)
Name is: a (after clicking on the button)
于 2013-08-03T21:02:48.927 回答
0

如果您需要知道对象的分配名称,那么最好将其作为构造函数调用约定的显式部分。例如:

        function obj = testClass(assignedName_input)
            obj.assignedName = assignedName_input;
            obj.handles_gui = obj.init_gui();
        end

然后,类的使用变为:

anyVariableName = testClass('test');  %Replaces "test = testClass();".  This separates the assigned name from the named used to keep track of it in the calling function.

或者,如果您稍后决定一次需要其中的 5 个,则在一个数组中

for ix = 1:5
    arrayOfObjects{ix} = testClass(['test_' num2str(ix)]);  %This is not possible if you only look at the assigned variable name.
end

如果出于某种原因(我能想到几个)您想确保每个实例只assignedName存在一个,您可以在类中使用静态containers.Map来维护现有对象的列表,并使用静态工厂方法来创建他们。(我很确定这是“工厂方法设计模式”。)

例如,添加:

properties(Access = private, Static = true)
    existingInstancesMap = containers.Map('keyType','char','valueType','any');
end

methods(Access = public, Static = true) 
    function mappedInstance = getInstance(assignedName);
    if ~existingInstancesMap.isKey(assignedName)                      %If no mapped instance exists
        existingInstancesMap(assignedName) = testClass(assignedName); %Then make one and map it.
    end
    mappedInstance = existingInstancesMap(assignedName);               %Return the mapped instance
end

然后将您的构造函数设为私有。

于 2013-08-02T23:05:40.173 回答