如何在 Matlab 中使对象在其 class 内可访问?
How to make an object accessible within its class, in Matlab?
在这个 class 中,我怎样才能让 fig
对象在其 class 中提供自己的相关属性,例如 my_function
;
classdef Test
properties
a
b
end
methods
function obj = Test(a, b)
obj.a = a;
obj.b = b;
end
function [] = my_function(obj)
fig.Name %%% here fig object is needed
disp('done!')
end
function [fig] = my_figure(obj)
fig = figure();
end
end
end
您需要将 fig
存储为 class 的 属性,然后从 my_function
中访问 fig
属性 当前实例。作为旁注,如果您希望能够通过引用传递您的 class 实例,您需要子 class MATLAB 的 handle
class:
classdef Test < handle
properties
fig % Setup a property to hold the handle to the figure
a
b
end
methods
function obj = Test(a, b)
obj.a = a;
obj.b = b;
end
function [] = my_function(obj)
% Access and modify the figure handle as needed
obj.fig.Name = 'Name';
disp('done!')
end
function [fig] = my_figure(obj)
fig = figure();
% Store the handle in the "fig" property of the class
obj.fig = fig;
end
end
end
在这个 class 中,我怎样才能让 fig
对象在其 class 中提供自己的相关属性,例如 my_function
;
classdef Test
properties
a
b
end
methods
function obj = Test(a, b)
obj.a = a;
obj.b = b;
end
function [] = my_function(obj)
fig.Name %%% here fig object is needed
disp('done!')
end
function [fig] = my_figure(obj)
fig = figure();
end
end
end
您需要将 fig
存储为 class 的 属性,然后从 my_function
中访问 fig
属性 当前实例。作为旁注,如果您希望能够通过引用传递您的 class 实例,您需要子 class MATLAB 的 handle
class:
classdef Test < handle
properties
fig % Setup a property to hold the handle to the figure
a
b
end
methods
function obj = Test(a, b)
obj.a = a;
obj.b = b;
end
function [] = my_function(obj)
% Access and modify the figure handle as needed
obj.fig.Name = 'Name';
disp('done!')
end
function [fig] = my_figure(obj)
fig = figure();
% Store the handle in the "fig" property of the class
obj.fig = fig;
end
end
end