structure in class

3 次查看(过去 30 天)
Christof
Christof 2012-6-14
Hi. I have a class with a property values constructor looks like this
the constructor is called with a cell of variable lengths. now I need to store data arrays of different length in the property value for each element in the cell. the data will be called from a db with using a method. how can I do that? I tried something like
function obj = myClass(names) %names as cell,e.g. ('a','b'}
obj.values = struct(names)
end
However, this throws an error Conversion to struct from cell is not possible.
any idea how to work around that? if there is something fundamentally stupid about my approach, please let me know how to do that in a smarter way. cheers, Chris

回答(1 个)

Nathaniel
Nathaniel 2012-6-14
Have a look at inputParser. My typical class constructor goes something like this:
function obj = myclass(varargin)
p = inputParser;
p.addParamValue('property1', [], @isnumeric);
p.addParamValue('property2', [], @ischar);
...
p.parse(varargin{:}); % edited this
fnames = fields(obj);
for n=1:numproperties
obj.(fnames{n}) = p.Results.(fnames{n});
end
  1 个评论
Christof
Christof 2012-6-18
thanks for your help. not familiar with that inputparser, so will look into it. always good to find something new

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Argument Definitions 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by