How do i set class members by calling its own methods?

1 次查看(过去 30 天)
I wanted to create a class where i can implement a class and set its members by calling its own methods.
This is the example class here:
classdef MANOVA1
%MANOVA1 calculate manova1
% This class perform ANOVA1 on dataset
properties (GetAccess = public)
g;
end
properties (Access = private)
data;
end
properties (Dependent)
Test_statistics;
p_value;
end
methods
function obj = MANOVA1(filename)
if nargin == 1
obj.setData(filename);
end
if isempty(obj.g)
obj.setg();
end
end
function setData(obj, filename)
obj.data = struct2cell(load(filename));
end
function setg(obj, data)
if (nargin == 1)
data = obj.data;
end
obj.g = length(data);
end
end
end
I was trying to call the class to the dataset: problem_5_1 exercise, by this following code:
obs = MANOVA('dataset_problem_5_1.mat')
disp(obs.g)
But when i run this code, the values are empty, because the members inside the class wouln't set when calling from class' own methods. I could do that with C++ and Python.

回答(1 个)

Steven Lord
Steven Lord 2022-10-9
By default, classes in MATLAB are value classes. See this documentation page for a discussion of the differences between value classes and handle classes. Also see this documentation page for a discussion of how classes in MATLAB behave differently from classes in other object-oriented languages.

类别

Help CenterFile Exchange 中查找有关 Migrate GUIDE Apps 的更多信息

产品


版本

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by