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:
Create empty dataset and populate it row by row

Subject: Create empty dataset and populate it row by row

From: Mark

Date: 24 Oct, 2011 14:55:14

Message: 1 of 3

Hi,

I'm trying to create an object of the class dataset (Stats toolbox) with a few named "columns". I want such object to initially have no rows. Then, I would like to add new rows one by one. I've tried the code below to create the dataset but I get an error. Can anyone help me with this?

Thanks in advance

P.S. I could not submit this question to the "answers" section of the Mathworks website

  % Column names are 'F1' and 'F2'
  myDS = dataset([],'VarNames',{'F1','F2'})
  
  ??? Error using ==> setvarnames at 23
  NEWNAMES must have one name for each variable in A.
  
  Error in ==> dataset.dataset>dataset.dataset at 383
              a = setvarnames(a,varnames); % names will be modified to make them valid

Subject: Create empty dataset and populate it row by row

From: Mark

Date: 24 Oct, 2011 22:28:16

Message: 2 of 3

Thanks to Fangjun Jiang, who kindly provided the answer to my question about creating an empty dataset:

 VarNames={'F1','F2'};
 myDS=dataset([],[],'VarNames',VarNames)

I am posting it here so that people who search USENET groups can find this solution



"Mark" wrote in message <j83u8i$i9c$1@newscl01ah.mathworks.com>...
> Hi,
>
> I'm trying to create an object of the class dataset (Stats toolbox) with a few named "columns". I want such object to initially have no rows. Then, I would like to add new rows one by one. I've tried the code below to create the dataset but I get an error. Can anyone help me with this?
>
> Thanks in advance
>
> P.S. I could not submit this question to the "answers" section of the Mathworks website
>
> % Column names are 'F1' and 'F2'
> myDS = dataset([],'VarNames',{'F1','F2'})
>
> ??? Error using ==> setvarnames at 23
> NEWNAMES must have one name for each variable in A.
>
> Error in ==> dataset.dataset>dataset.dataset at 383
> a = setvarnames(a,varnames); % names will be modified to make them valid

Subject: Create empty dataset and populate it row by row

From: Peter Perkins

Date: 25 Oct, 2011 02:53:23

Message: 3 of 3

As with any MATLAB array, performance is usually best if you can
"preallocate". In this case, you might do something like

VarNames = {'F1','F2'};
myDS = dataset(zeros(n,1),zeros(n,1),'VarNames',VarNames)

...
myDS.F1(i) = ...
myDS.F2(i) = ...

It's not _required_, but you may find that things work much faster if
you know in advance how many rows you'll need.


On 10/24/2011 6:28 PM, Mark wrote:
> Thanks to Fangjun Jiang, who kindly provided the answer to my question
> about creating an empty dataset:
>
> VarNames={'F1','F2'};
> myDS=dataset([],[],'VarNames',VarNames)
>
> I am posting it here so that people who search USENET groups can find
> this solution
>
>
>
> "Mark" wrote in message <j83u8i$i9c$1@newscl01ah.mathworks.com>...
>> Hi,
>>
>> I'm trying to create an object of the class dataset (Stats toolbox)
>> with a few named "columns". I want such object to initially have no
>> rows. Then, I would like to add new rows one by one. I've tried the
>> code below to create the dataset but I get an error. Can anyone help
>> me with this?
>>
>> Thanks in advance
>>
>> P.S. I could not submit this question to the "answers" section of the
>> Mathworks website
>>
>> % Column names are 'F1' and 'F2'
>> myDS = dataset([],'VarNames',{'F1','F2'})
>> ??? Error using ==> setvarnames at 23
>> NEWNAMES must have one name for each variable in A.
>> Error in ==> dataset.dataset>dataset.dataset at 383
>> a = setvarnames(a,varnames); % names will be modified to make them valid

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