如果我正确理解您的问题,答案可能取决于您如何创建实例化对象的类。在最新版本的 MATLAB 中,可以通过两种方式定义类:“值”类或“句柄”类(此处的 MATLAB 文档)。从文档中引用:
下面的示例代码提供了一些示例,说明如何与上面描述的“嵌套”对象进行交互,包括值类嵌套对象和句柄类嵌套对象:
% For value classes:
objC = C(...); % Make an object of class C, where "..." stands
% for any input arguments
objB = B(...,objC); % Make an object of class B, passing it objC
% and placing objC in field 'objC'
objA = A(...,objB); % Make an object of class A, passing it objB
% and placing objB in field 'objB'
% If the '.' operator (field access) is defined for the objects:
objA.objB.objC.D = 1; % Set field 'D' in objC to 1
objA.objB.objC = foo(objA.objB.objC,...); % Apply a method that
% modifies objC and
% returns the new
% object
% For handle classes:
hC = C(...); % Get a handle (reference) for a new object of class C
hB = B(...,hC); % Get a handle for a new object of class B,
% passing it handle hC and placing it in field 'hC'
hA = A(...,hB); % Get a handle for a new object of class A,
% passing it handle hB and placing it in field 'hB'
% If the '.' operator (field access) is defined for the objects:
hC.D = 1; % Set field 'D' to 1 for object referenced by hC; Note
% that hC and hA.hB.hC both point to same object, and
% can thus be used interchangably
foo(hC); % Apply a method that modifies the object referenced by hC
% If instead using get/set methods for the handle object:
set(hC,'D',1);
set(get(get(hA,'hB'),'hC'),'D',1); % If variable hC wasn't made, get
% reference from nested objects
foo(hC);
foo(get(get(hA,'hB'),'hC'));
如您所见,使用句柄类可以通过将句柄(本质上是指针)的副本存储在另一个变量中来帮助您避免将函数调用和字段引用链接起来。句柄类还消除了用对这些对象进行操作的方法返回的新副本覆盖对象的旧副本的需要。
希望这对您的要求有所帮助。