Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Question about using datasetfun(@(x,y)

Subject: Question about using datasetfun(@(x,y)

From: Kevin Ellis

Date: 11 Jan, 2013 18:12:08

Message: 1 of 3

Hello,

I had a quick question about using "datasetfun" where the function includes different variables from different columns in a dataset. My dataset is <57617x29> and called SASDoc32. The columns of interest look like:

SASDoc32.DocumentNumber(1:5)

ans =

'32115466B2510'
'32119532F2370'
'32116183B2360'
'32112922F2550'
'32115841F2520'

SASDoc32.DocumentSuffix(1:5)

ans =

'001'
'001'
'001'
'001'
'001'

This is only a portion of the dataset. Anyways, all I want to do is append the DocumentSuffix to the DocumentNumber (i.e. for the first entry: '32115466B2510001') in a separate column of the dataset. So I actually have two questions:

1) How to add a new column to a dataset without receiving all these warnings? I have been just assigning values to them and always receive the following warning:

Warning: Observations with default values added to dataset variable 'DocNo'

So is there a proper way to add a column to a dataset? My method works, but warnings are annoying and seem to suggest a better way.

2) My most important question is appending the DocumentSuffix to the DocumentNumber. I have tried using datasetfun in a way cellfun should work. My attempt at this is given by:

SASDoc32(:,'DocNo') = datasetfun(@(x,y) [x,y],'DataVars',{'DocumentNumber','DocumentSuffix'},'DatasetOutput',false);

I always receive the error that I don't have enough input arguments. Does datasetfun not support the @(x,y) command? Any help would be much appreciated. Thanks.

Kevin Ellis

Subject: Question about using datasetfun(@(x,y)

From: james bejon

Date: 11 Jan, 2013 21:29:09

Message: 2 of 3

Datasets can be a bit fiddly at times.

I don't have Matlab to hand so will have to go by memory.

In my opinion, the key to working with datasets is to understand that indexing a dataset via normal indexing methods returns a dataset but indexing it via a "dot" extension can return a single column as a non-dataset item, e.g.:

% Define a dataset as follows [hopefully I've got this right]
ds = dataset( {rand(10, 1), repmat({'strs'}, 10, 1) }, 'VarNames', {'A', 'B'} )

% Now, the following syntax returns a vector of doubles
X = ds.A;
whos X

% But this syntax returns a dataset
X = ds(:, 1);
whos X

% Which means this is valid:
ds.A = rand(10, 1) * 5;

% But this isn't:
ds(:, 1) = rand(10, 1) * 5;
% because you've got a dataset on the LHS but a vector of double on the RHS

% So to concat:
ds.B = strcat(ds.B, {' and '}, ds.B);
disp(ds)

Subject: Question about using datasetfun(@(x,y)

From: Kevin Ellis

Date: 13 Jan, 2013 02:18:18

Message: 3 of 3

"james bejon" wrote in message <kcq075$ant$1@newscl01ah.mathworks.com>...
> Datasets can be a bit fiddly at times.
>
> I don't have Matlab to hand so will have to go by memory.
>
> In my opinion, the key to working with datasets is to understand that indexing a dataset via normal indexing methods returns a dataset but indexing it via a "dot" extension can return a single column as a non-dataset item, e.g.:
>
> % Define a dataset as follows [hopefully I've got this right]
> ds = dataset( {rand(10, 1), repmat({'strs'}, 10, 1) }, 'VarNames', {'A', 'B'} )
>
> % Now, the following syntax returns a vector of doubles
> X = ds.A;
> whos X
>
> % But this syntax returns a dataset
> X = ds(:, 1);
> whos X
>
> % Which means this is valid:
> ds.A = rand(10, 1) * 5;
>
> % But this isn't:
> ds(:, 1) = rand(10, 1) * 5;
> % because you've got a dataset on the LHS but a vector of double on the RHS
>
> % So to concat:
> ds.B = strcat(ds.B, {' and '}, ds.B);
> disp(ds)

I finally got around to checking your solution. It works perfectly without any warnings too. Still new to datasets so it was refreshing to actually have a correct answer for once. Thanks again James.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us