Filter löschen
Filter löschen

structure in class

3 Ansichten (letzte 30 Tage)
Christof
Christof am 14 Jun. 2012
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

Antworten (1)

Nathaniel
Nathaniel am 14 Jun. 2012
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 Kommentar
Christof
Christof am 18 Jun. 2012
thanks for your help. not familiar with that inputparser, so will look into it. always good to find something new

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Argument Definitions 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!

Translated by