dataset array - how can new data be added from a cell array that has some missing elements

I've got numeric data in a cell array that has some empty elements, and I'd like to add this to a dataset array. If I convert to a matrix first, the indexing will be incorrect. I've tried with no avail the customary ways of assignmet to structures:
1) [dataset.prop] = propCellArray{:}; % this only copies 1st cell contents only
[dataset.prop] = deal(propCellArray); % contents remain cell arrays, and any attempt to convert to mat causes indexing problems
Anyone know the correct syntax?
thanks,
Aaron

 Respuesta aceptada

dataset=struct('prop',propCellArray);
If dataset already exists, use
[dataset.prop]=deal(propCellArray{:})
Okay, don't use dataset as variable name, especially when you are using the dataset() array.
If constructing from new:
propCellArray={1 [] 3 [] 5};
d1=dataset({propCellArray','prop'})
If modifying existing dataset array:
d2=dataset({rand(5,1),'prop'});
idx=cellfun('isempty',propCellArray);
d2.prop(~idx)=[propCellArray{:}]'
Or do processing on propCellArray first and then assign
propCellArray(idx)={0};
d2.prop=[propCellArray{:}]'

4 comentarios

Thanks for your answer, but deal complains that 'The number of outputs should match the number of inputs.'. Keep in mind that I am using the built in class 'dataset' and not a 'struct'. Your approach (and the one I showed above) work for structs, but not for datasets for some reason.
[dataset(1:numel(propCellArray)).prop] = deal(propCellArray{:});
perhaps
@Walter:
dtset = dataset();
[dtset(1:numel(propCellArray)).prop] = deal(propCellArray{:});
??? Error using ==> dataset.subsasgn at 83
Dataset array subscripts must be two-dimensional.
Then:
[dtset(1:numel(propCellArray),'prop')] = deal(propCellArray{:});
??? Error using ==> deal at 38
The number of outputs should match the number of inputs.
Ah... I haven't worked with datasets.

Iniciar sesión para comentar.

Más respuestas (1)

% Create sample cell array
propCellArray = num2cell(rand(20,1));
propCellArray(randi(20,3,1)) = {[]};
% Create sample dataset
dtset = dataset({[],'Prop'});
% Index non-empty and replace with NaN
propCellArray(cellfun('isempty',propCellArray)) = {NaN};
dtset.Prop = cat(1,propCellArray{:});
Other ways will give you dtset.Prop as one element containing the whole propCellArray converted to double and by default padded with zeros where it was empty.

1 comentario

Thanks Oleg. I think your approach is the best. I was trying to avoid NaN, as (in my experience) many functions will return NaN if any element in the input is NaN (mean(), std(), ....), but it looks like the Statistics toolbox functions deal with such entries in a more convenient way. I would have liked to 'accept' your answer, but the option is no longer there, so have an upvote instead.

Iniciar sesión para comentar.

Categorías

Community Treasure Hunt

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

Start Hunting!

Translated by