Constructor for a class, that accepts name-value pairs for setting properties
27 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Alex Alex
am 17 Jun. 2021
Kommentiert: Alex Alex
am 17 Jun. 2021
Hello! Could anyone advise on how to write a class constructor, that accepts name-value pairs for setting properties? For the untouched properties it must use predefined values. Using like this:
X = classX('Prop1',val1,'Prop3',val3)
Thanks
0 Kommentare
Akzeptierte Antwort
Matt J
am 17 Jun. 2021
Bearbeitet: Matt J
am 17 Jun. 2021
With a recent Matlab version, you can do,
classdef classX
properties
Prop1, Prop3;
end
methods
function obj=classX(namedArgs)
arguments
namedArgs.Prop1=default1;
namedArgs.Prop3=default3;
end
obj.Prop1=namedArgs.Prop1;
obj.Prop3=namedArgs.Prop3;
end
end
end
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!