Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何创建一个;“单向依赖性”;在Matlab句柄类中_Matlab_Handle_Matlab Class - Fatal编程技术网

如何创建一个;“单向依赖性”;在Matlab句柄类中

如何创建一个;“单向依赖性”;在Matlab句柄类中,matlab,handle,matlab-class,Matlab,Handle,Matlab Class,我对Matlab相当陌生,我想知道是否有一种方法可以创建“单向句柄类” 为了更好地解释,假设我有一个名为test_class的类,其属性为“prop1”和“prop2” 我希望应用于test_1(父级)中的属性的更改会影响test_2(子级),但我不希望test_2中的更改会影响test_1,所以 test_1.prop1 = 20; test_2.prop1: 20 test_2.prop2 = 30; test_1.prop2: 5 有没有办法创造这种“单向依赖” 提前谢谢 我认为这是不

我对Matlab相当陌生,我想知道是否有一种方法可以创建“单向句柄类”

为了更好地解释,假设我有一个名为test_class的类,其属性为“prop1”和“prop2”

我希望应用于test_1(父级)中的属性的更改会影响test_2(子级),但我不希望test_2中的更改会影响test_1,所以

test_1.prop1 = 20;
test_2.prop1: 20

test_2.prop2 = 30;
test_1.prop2: 5
有没有办法创造这种“单向依赖”


提前谢谢

我认为这是不可能的。您可以拥有引用相同基础对象(双向相关)的句柄对象副本,也可以拥有副本相互独立的值对象副本


您可能能够创建具有包含句柄的
属性的值对象。。。物体的一部分是双向的,另一部分是单向的。但这并不是您真正想要的。

这里是一个基本实现。每个对象都有一个父对象和一个子对象数组。使用
subsasgn
我们可以更改对象及其子对象的属性,因为对象是单向的,所以我们不想更改父对象的属性

用法:

a = oneway(1,2);
b = oneway(a);
c = oneway(b);
如果我们设置
a.prop1=7然后将更改
b
,从而导致
c
的更改。如果只想更改直接子项,可以取消注释行
31
和注释行
30

classdef oneway < handle
    properties
        parent
        children={};
    end
    properties
     prop1
     prop2
    end
    methods
        function obj = oneway(varargin)
            if nargin == 1
                a = varargin{1};
                if isa(a,'oneway')
                    obj.prop1 = a.prop1;
                    obj.prop2 = a.prop2;
                    obj.parent = a;
                    a.children{end+1} = obj;
                end
            elseif nargin == 2
                obj.prop1 = varargin{1};
                obj.prop2 = varargin{2};
            end
        end
        function obj = subsasgn(self, S, B)
            if strcmp(S.type, '.')
                if ismember(S.subs, properties(self))
                    obj = builtin('subsasgn', self, S, B);
                    for k = 1: numel(self.children)
                        self.children{k} = subsasgn(self.children{k},S,B);
                        %self.children{k} = builtin('subsasgn', self.children{k}, S, B);
                    end
                end
            end
        end
        function delete(self)
            self.parent.children (cellfun(@(x)x==self,self.parent.children))=[];
            for k = 1: numel(self.children)
                self.children{k}.parent =[];
            end
        end
    end
end
classdef单向
通过利用属性集侦听器,您可以做到这一点,而不必陷入子SGN的困难。这样,你就不必去管所有的子副本了。这看起来像如下所示:

classdef test_class < matlab.mixin.Copyable

    properties(SetObservable)
        prop1
        prop2
    end

    properties
        prop3
    end

    methods
        function obj = test_class(in1, in2, in3)
            obj.prop1 = in1;
            obj.prop2 = in2;
            obj.prop3 = in3;
        end
        function ref = make_dependent_reference(obj)
            ref = copy(obj);

            cls = metaclass(obj);
            observableProps = cls.PropertyList.findobj('SetObservable',true);
            for ct =1:numel(observableProps)
                obj.addlistener(observableProps(ct).Name, 'PostSet', ...
                    @(prop,evd)ref.update_dependent_reference(prop,evd));
            end
        end
    end
    methods(Access=private)
        function update_dependent_reference(ref, prop, evd)
            ref.(prop.Name) = evd.AffectedObject.(prop.Name);
        end
    end
end

它非常优雅!
classdef test_class < matlab.mixin.Copyable

    properties(SetObservable)
        prop1
        prop2
    end

    properties
        prop3
    end

    methods
        function obj = test_class(in1, in2, in3)
            obj.prop1 = in1;
            obj.prop2 = in2;
            obj.prop3 = in3;
        end
        function ref = make_dependent_reference(obj)
            ref = copy(obj);

            cls = metaclass(obj);
            observableProps = cls.PropertyList.findobj('SetObservable',true);
            for ct =1:numel(observableProps)
                obj.addlistener(observableProps(ct).Name, 'PostSet', ...
                    @(prop,evd)ref.update_dependent_reference(prop,evd));
            end
        end
    end
    methods(Access=private)
        function update_dependent_reference(ref, prop, evd)
            ref.(prop.Name) = evd.AffectedObject.(prop.Name);
        end
    end
end
>> t = test_class(5,10,15)

t = 

  test_class with properties:

    prop1: 5
    prop2: 10
    prop3: 15

>> ref = t.make_dependent_reference

ref = 

  test_class with properties:

    prop1: 5
    prop2: 10
    prop3: 15

>> ref.prop1 = 6

ref = 

  test_class with properties:

    prop1: 6
    prop2: 10
    prop3: 15

>> t

t = 

  test_class with properties:

    prop1: 5
    prop2: 10
    prop3: 15

>> t.prop2 = 11

t = 

  test_class with properties:

    prop1: 5
    prop2: 11
    prop3: 15

>> ref

ref = 

  test_class with properties:

    prop1: 6
    prop2: 11
    prop3: 15