From: John Roberts on
I've just started using the dataset type from the Statistics toolbox, and I've found it quite useful. I'm having some problems though with some basic manipulation. As an example,

snames = nominal({'setosa';'versicolor';'virginica'});
CC = dataset({snames,'species'},{[38;108;70],'cc'})

1) How can I convert chosen elements of the dataset to a string/cell? Let's say I want to take CC(1,1) and do some string manipulations and put it back in? This would be the equivalent of single/double dataset methods but applicable to strings.

2) How can I print out a data set to a file? I'm used to writing out using fopen and fprintf, but the function is not defined for dataset inputs. What's the preferred method for doing this?

Thanks,

John
From: Peter Perkins on
On 3/12/2010 7:59 PM, John Roberts wrote:
> 1) How can I convert chosen elements of the dataset to a string/cell?
> Let's say I want to take CC(1,1) and do some string manipulations and
> put it back in? This would be the equivalent of single/double dataset
> methods but applicable to strings.

The species variable in CC is a nominal vector, so I'm not sure what sort of string operations you want to do. But in general, if you want to work on the "raw" data in a dataset array, you want to use "dot variable" subscripting, something like this:

>> snames = nominal({'setosa';'versicolor';'virginica'});
>> CC = dataset({snames,'species'},{[38;108;70],'cc'})
CC =
species cc
setosa 38
versicolor 108
virginica 70
>> CC.species(1)
ans =
setosa
>> CC.species(1) = 'versicolor'
CC =
species cc
versicolor 38
versicolor 108
virginica 70

CC(1,1), on the other hand, is a 1x1 dataset array, with one variable (species) and only one observation:

>> CC(1,1)
ans =
species
setosa

Parenthesis subscripting is usually most useful for getting at rectangular (or "plaid") subarrays, rather than getting at individual values or variables.


> 2) How can I print out a data set to a file? I'm used to writing out
> using fopen and fprintf, but the function is not defined for dataset
> inputs. What's the preferred method for doing this?

Probably you want to use the EXPORT method:

>> export(CC,'File','CC.dat','Delimiter',',')
>> type CC.dat

species,cc
setosa,38
versicolor,108
virginica,70


You can use other delimiters (default is tab), and also write to an Excel file. Hope this helps.
From: John Roberts on
Perfect. Thanks for your help.
From: John Roberts on
Actually, is there a way to get export to append to a file instead of overwriting? Is there another way to write out data where I still have control of the file?
 | 
Pages: 1
Prev: sign test, polarity
Next: viewing structure arrays