フィルターのクリア

Constructor for a class, that accepts name-value pairs for setting properties

29 ビュー (過去 30 日間)
Alex Alex
Alex Alex 2021 年 6 月 17 日
コメント済み: Alex Alex 2021 年 6 月 17 日
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

採用された回答

Matt J
Matt J 2021 年 6 月 17 日
編集済み: Matt J 2021 年 6 月 17 日
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
  1 件のコメント
Alex Alex
Alex Alex 2021 年 6 月 17 日
Thank you Matt, this is exactly what I asked. I tried and it works.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeConstruct and Work with Object Arrays についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by