Borrar filtros
Borrar filtros

structure in class

3 visualizaciones (últimos 30 días)
Christof
Christof el 14 de Jun. de 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

Respuestas (1)

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

Iniciar sesión para comentar.

Categorías

Más información sobre Argument Definitions en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by