How to convert dataset variables from cell?
조회 수: 1 (최근 30일)
이전 댓글 표시
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?)...
댓글 수: 0
답변 (2개)
Peter Perkins
2013년 2월 4일
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.
댓글 수: 0
Daniell Algar
2013년 2월 2일
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
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Type Conversion에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!