How to convert dataset variables from cell?

3 views (last 30 days)
I've got a dataset object called data.
data =
subject subid sex age
'rachel' 's1' 'f' 21
'tom' 's2' 'm' 24
-----------------------------------------------------------------------------
QUESTION 1
If I check the class of any of my variables, they're all cell:
class(data.subject), ans=cell
I can't seem to figure out how to convert these from cell to a sensible format for indexing.
For example, if I try:
>> nominal(data.sex)
Error using categorical (line 110) Could not compute unique values in A using UNIQUE.
Error in nominal (line 93) b = b@categorical(varargin{:});
Caused by: Error using cell/unique (line 95) Input A must be a cell array of strings.
And it won't let me convert data.sex to a cell array of strings...
>> cellstr(Testdata.sex) Error using cellstr (line 34) Input must be a string.
>> str(Testdata.sex) Undefined function 'str' for input arguments of type 'cell'.
Any suggestions?
Is this part of the problem?
>> data.subject
{1x1 cell}
{1x1 cell}
-----------------------------------------------------------------------------
QUESTION 2
I've also got a dataset variable with [x,y,z] data in the following form (also as a cell):
'[4.55566 4.33454 3.45345]' '[3.34534 3.44534 2.23423]'
I'd like to round the values and convert it to something like [4,4,3] [3,3,2]
as a double (maybe?)...

Answers (2)

Peter Perkins
Peter Perkins on 4 Feb 2013
This
>> nominal(data.sex)
Error using categorical (line 110) Could not compute unique values in A using UNIQUE.
Error in nominal (line 93) b = b@categorical(varargin{:});
Caused by: Error using cell/unique (line 95) Input A must be a cell array of strings.
is probably because you have something other than strings in that particular dataset variable. UNIQUE on a cell array is only defined when all the cells contain strings. For example
>> unique({'m' 'm' 'f' []})
Error using cell/unique (line 86)
Input A must be a cell array of strings.
And this
>> data.subject {1x1 cell} {1x1 cell}
makes me think that you have a cell array each of whose cells contains a 1x1 cell array, which then contains a string. Compare
>> d = dataset({'rachel'; 'tom'},{'s1'; 's2'},{'f'; 'm'},[21; 24],'VarNames',{'subject','subid','sex' 'age'})
d =
subject subid sex age
'rachel' 's1' 'f' 21
'tom' 's2' 'm' 24
to
>> d = dataset({{'rachel'}; {'tom'}},{'s1'; 's2'},{'f'; 'm'},[21; 24],'VarNames',{'subject','subid','sex' 'age'})
d =
subject subid sex age
{1x1 cell} 's1' 'f' 21
{1x1 cell} 's2' 'm' 24
These
'[4.55566 4.33454 3.45345]' '[3.34534 3.44534 2.23423]'
are strings. I imagine you want numeric values. If all of the observations in this dataset have three values, then you probably want to store the values as a double matrix with three columns. If there can be different lengths of values for different observations, then you want a cell array, each cell of which contains a double vector.
To clean this all up, you probably want to start over, or use some version of cellfun. For example,
data.subject = cellfun(@(c)c{:},data.subject,'UniformOutput',false);
data.x = cell2mat(cellfun(@(s)str2num(s),data.x,'UniformOutput',false))
(where x is the name of the variable you did not provide a name for). Needless to say, this is a bit arcane, because your data are in such a funny form. I don't know how your data got into these funny forms, so I can't really say how to avoid it.
Hope this helps.

Daniell Algar
Daniell Algar on 2 Feb 2013
If I understand your data correct, then it's more a question of how you acces the data, than how to convert the data. For instance, take the second question. The following
a= {[4.55566 4.33454 3.45345], [3.34534 3.44534 2.23423]}
would give you the same cell as you have? To perform operands on it, use the cell brackets, as e.g.
a{1}
ans =
4.5557 4.3345 3.4535
Also, you can access individual elements as usual, ones you've pointed to what part of the cell you are interested in, i.e.
a{1}(1)
ans =
4.5557
So, to do the rounding you mentioned, just
floor(a{1})
ans =
4 4 3
The same applies to the data you had in the beginning of your text.
Hope this helps. Good luck

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!