Why are strings stored in an extra cell within a table
20 次查看(过去 30 天)
显示 更早的评论
Hi,
Why are strings stored in an extra cell when placed in a table so that one has to add an extra {1} to access the string? I run into this issue, e.g., when importing data tables with strings using the 'ImportData' interface. Small example (not using the importing function):
x = {'FirstString' 'SecondString' ''};
xTable = cell2table(x);
out1 = class(xTable)
out2 = class(xTable(1,1))
out3 = class(xTable{1,1})
out1 =
table
out2 =
table
out3 =
cell
out1 and out2 make sense. But why is out3 = cell? Why not 'char'? E.g., if I want to find out that the 3rd string is empty, I have to do this:
out4 = isempty(xTable{1,3}{1})
which, I feel, is rather impractical.
What is the reason for this design? Or do I do something wrong and I can directly place strings into a table? Thank you for your help!
0 个评论
采纳的回答
Guillaume
2017-1-27
The reason why xTable{1,1} is a scalar cell array is due to the way the data is stored internally in the table. The whole column is stored internally as a cell array and since individual cells of a cell array can contain anything, including several strings, they must be returned as cell arrays.
Similar to your example, consider
x = {'FirstString'; {'SecondString', ''}; ''};
xtable = cell2table(x);
xtable{2, 1} %has to be returned as cell array since it contains two strings.
Note that with your example, there is a very simple way to test if a cell is empty, using ismissing:
x = {'FirstString'; 'SecondString'; ''};
xtable = cell2table(x);
ismissing(xtable)
Also, since R2016b, there's now a much better way to store strings, using the new string class. matlab will do what you expected if you use that to store your strings.
x = {'FirstString'; 'SecondString'; ''};
xstring = string(x);
xtable = array2table(xstring)
class(xtable{1, 1}) %return string not cell
xtable{3,1} == '' %test for empty string. DO NOT USE isempty
更多回答(1 个)
Peter Perkins
2017-1-27
As the other replies have sort of hinted at but not said explicitly, this actually has little to do with tables, although using a table maybe makes it more obvious.
As others have said, the standard way (up until R2016b, see below) to store text strings in MATLAB is as "a cell array of char vectors", like this:
>> c = {'a'; 'ab'; 'abc'}
c =
'a'
'ab'
'abc'
Notice that the three strings are different lengths. You can store those in a char matrix, but you probably don't want to because you'd have to pad the shorter ones with spaces. That's the reason for putting the char vectors in a cell array. And table, readtable, and the Import Tool use that standard.
>> t = table(c)
t =
c
_____
'a'
'ab'
'abc'
So to get at an individual string, i.e. the char vector, in the cell array in the table, you do the same thing you'd do with the cell array if it were not in the table, but with the table prepended:
>> c{1}
ans =
'a'
>> t.c{1}
ans =
'a'
I think maybe what's confusing you is that if you use braces on the table, you need two sets of braces, one to get "into" the table to the cell array, and another to get "into" the cell array to the char vector. In other words, this
>> t{1,1}
ans =
cell
'a'
has not dug deeply enough. If you're working on one variable in a table, dot subscripting is equivalent to braces on the table, but often easier to understand.
Starting in R2016b, the new string data type was introduced. So now,
>> s = string(c)
s =
"a"
"ab"
"abc"
>> t = table(s)
t =
s
_____
"a"
"ab"
"abc"
Unlike a "cell array of char vectors", the type of one element of a string array is the thing you want, a "string". So this
>> t{1,1}
ans =
"a"
is a 1x1 string array, and you don't need to dig any deeper.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!