MATLAB: Are strings stored in an extra cell within a table

stringstable

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!

Best Answer

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
Not that if you use string see test for empty strings