Parse error at 'class' 'properties' 'methods' and 'end', "usage might be invalid syntax"

조회 수: 4 (최근 30일)
Gabriel McQueen
Gabriel McQueen 2021년 7월 19일
댓글: Steven Lord 2021년 7월 19일
Unsure where I went wrong, any help would be greatly appreciated.
code:
classdef sData
%declaration of sample data
properties
sId;
sLength;
sRadius;
sDensity;
sGold;
sSulfur;
sArsenic;
end
properties (Dependent)
goldW;
sulfurW;
arsenicW;
end
methods
%objection definition titled sample
function sample = sData(id, l, r, d, g, s, a, gW, sW, aW)
sample.sId = id;
sample.sLength = l;
sample.sRadius = r;
sample.sDensity = d;
sample.sGold = g;
sample.sSulfur = s;
sample.sArsenic = a;
sample.goldW = gW;
sample.sulfurW = sW;
sample.arsenicW = aW;
end
%sample total weight function
function [sampleWeight] = weight(sample)
volume = sample.sLength*sample.sRadius^2*pi;
sampleWeight = volume*sample.sDensity;
end
%gold, sulfur, and arsenic weight calculation
function [gW, sW, aW] = concentrations(sample)
gW = gold/weight(sample);
sW = sulfur/weight(sample);
aW = arsenic/weight(sample);
end
end
end

답변 (1개)

Steven Lord
Steven Lord 2021년 7월 19일
The name of the attribute is Dependent not Dependant.
  댓글 수: 2
Gabriel McQueen
Gabriel McQueen 2021년 7월 19일
I corrected the spelling mistake but I get the same errors, do you have advice on this?
Steven Lord
Steven Lord 2021년 7월 19일
Once I made that correction and tried to create an sData object I did receive an error, though it's a different one than you described.
>> y = sData('abc', 1, 2, 3, 4, 5, 6, 7, 8, 9)
In class 'sData', no set method is defined for Dependent property 'goldW'. A Dependent property needs a
set method to assign its value.
Error in sData (line 27)
sample.goldW = gW;
If I had a Circle class with a regular Radius property and a Dependent Area property (that I computed using the Radius whenever the user asked for it), in order to support the user setting a Circle's Area I would need to compute the corresponding Radius and update that property of the Circle.
You need to define property set methods for those three Dependent properties that updates one or more of the properties upon which the Dependent property depends. It would look something like this:
methods
function obj = set.goldW(obj, newValue)
end
end
You'll probably also want to define property get methods to be able to retrieve the values of those properties.
The Circle class I described above would have get.Area and set.Area methods, the former of which returns pi*obj.Radius.^2 and the latter of which would set obj.Radius to sqrt(newvalue./pi).

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 .NET Data Types in MATLAB에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by