structure in class

2 visualizzazioni (ultimi 30 giorni)
Christof
Christof il 14 Giu 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

Risposte (1)

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

Accedi per commentare.

Categorie

Scopri di più su Argument Definitions in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by