필터 지우기
필터 지우기

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