Got Questions? Get Answers.
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:
Help using datasetfun

Subject: Help using datasetfun

From: Kevin Ellis

Date: 30 Dec, 2012 23:03:10

Message: 1 of 2

Hello,

I have a large dataset (411,000 entries with 34 variables). My question involves using datasetfun to convert string variables into numbers. A portion of my dataset looks like this:

SAS.DocumentType(1:15)

ans =

    '27'
    '49'
    '28'
    '13'
    '32'
    '78'
    '15'
    '27'
    '35'
    '27'

All I want to do is convert these strings into numbers using str2num and datasetfun. I have tried many combinations of the following code to accomplish this:

SAS.DocumentType = datasetfun(@str2num,SAS.DocumentType);

I also seem to receive the following error message:

Undefined function 'datasetfun' for input arguments of type 'function_handle'.

I think I understand why I am receiving this error. The only way for str2num to work is if I pass it something in the {} brackets. For example, str2num(SAS.DocumentType{1}) works perfectly, but I want to do this for all 400,000 entries without using a FOR-loop. Could someone help me understand how to use datasetfun to accomplish this? Thanks.

Kevin

Subject: Help using datasetfun

From: Steven_Lord

Date: 31 Dec, 2012 03:38:38

Message: 2 of 2



"Kevin Ellis" <kevin.ellis86@gmail.com> wrote in message
news:kbqh7d$r6u$1@newscl01ah.mathworks.com...
> Hello,
>
> I have a large dataset (411,000 entries with 34 variables). My question
> involves using datasetfun to convert string variables into numbers. A
> portion of my dataset looks like this:
>
> SAS.DocumentType(1:15)

*snip*

> All I want to do is convert these strings into numbers using str2num and
> datasetfun. I have tried many combinations of the following code to
> accomplish this:
>
> SAS.DocumentType = datasetfun(@str2num,SAS.DocumentType);

The DATASETFUN function requires its second input to be a dataset object.
Your call does not satisfy that requirement; the second input argument is a
cell array of strings. I believe you have two main options:

1) Extract DocumentType to a separate cell array, process it using STR2NUM
or the like, then update the dataset. I'm using the hospital data set
included with Statistics Toolbox as an example.

load hospital
h1 = hospital(1:10,:);
ages = h1.Age;
agesCell = num2cell(ages);
h2 = h1;
h2.Age = agesCell;
h1, h2

2) You may be able to write your own function that operates on the dataset
object as a whole and use THAT function in DATASETFUN. It will be more
complicated than STR2NUM, however, and you'll probably want to use the
'DatasetOutput' option for DATASETFUN in this situation. I don't have an
example for this case.

> I also seem to receive the following error message:
>
> Undefined function 'datasetfun' for input arguments of type
> 'function_handle'.

See my first sentence.

*snip*

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

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