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

6 Ansichten (letzte 30 Tage)
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

Antworten (1)

Steven Lord
Steven Lord am 19 Jul. 2021
The name of the attribute is Dependent not Dependant.
  2 Kommentare
Gabriel McQueen
Gabriel McQueen am 19 Jul. 2021
I corrected the spelling mistake but I get the same errors, do you have advice on this?
Steven Lord
Steven Lord am 19 Jul. 2021
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).

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Entering Commands finden Sie in Help Center und File Exchange

Produkte


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by