Dot notation and curly braces with TABLE

28 views (last 30 days)
I don't understand clearly the difference between T.(expression) and T{:,expression} notation.
For example :
load patients
data = table(Gender,Age)
data = 100×2 table
Gender Age __________ ___ {'Male' } 38 {'Male' } 43 {'Female'} 38 {'Female'} 40 {'Female'} 49 {'Female'} 46 {'Female'} 33 {'Male' } 40 {'Male' } 28 {'Female'} 31 {'Female'} 45 {'Female'} 42 {'Male' } 25 {'Male' } 39 {'Female'} 36 {'Male' } 48
varfun(@class, data)
ans = 1×2 table
class_Gender class_Age ____________ _________ cell double
What's the matter with :
data{:,2} = uint8(data{:,2});
No error, no warning, everything seems OK, but Age is always double. Why ?
varfun(@class, data)
ans = 1×2 table
class_Gender class_Age ____________ _________ cell double
I understand that I must use :
data.(2) = uint8(data{:,2});
varfun(@class, data)
ans = 1×2 table
class_Gender class_Age ____________ _________ cell uint8
Why data.(2) is different of data{:,2} to modify the type of variables.
Thank you advance.
SAINTHILLIER Jean Marie
  1 Comment
Matt J
Matt J on 9 Jun 2023
Edited: Matt J on 9 Jun 2023
I have edited your post to display the code output that your words are referencing. It is advisable that you do this from now on, so that there is no guesswork for us as to what output you are seeing.

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 9 Jun 2023
Edited: Stephen23 on 10 Jun 2023
You can think of curly-brace indexing into a table as being something a bit like parenthesis indexing into a numeric array:
V = 1:7
V = 1×7
1 2 3 4 5 6 7
V(:) = uint8(11:17) % indexing into V does not change its class!
V = 1×7
11 12 13 14 15 16 17
In contrast, using dot indexing to assign to a table basically reassigns the entire variable, much like this:
V = uint8(11:17)
V = 1×7
11 12 13 14 15 16 17
This behavior is expected: curly-brace indexing can be used to index into some of the table rows, so we do not expect indexing using curly braces to change the data type in that table column/variable:
T = array2table((1:7).')
T = 7×1 table
Var1 ____ 1 2 3 4 5 6 7
T{3:5,1} = uint8(13:15).'
T = 7×1 table
Var1 ____ 1 2 13 14 15 6 7
class(T.Var1)
ans = 'double'
Clearly it would be inconsistent of MATLAB to suddenly change that behavior when all elements are being indexed into.
Dot indexing is quite a different thing, because users expect to be able to replace an entire table column/variable with
tbl.name = whatever
and it should work (even if there already exists a completely different, incompatiable class in a variable/column with that name)... in fact, given that requirement, this is the only interpretation of this syntax that is consistent with new table columns/variables and existing ones (otherwise the behavior would be different, depending on if that variable/column exists already or not).
T.Var1 = uint8(11:17).'
T = 7×1 table
Var1 ____ 11 12 13 14 15 16 17
class(T.Var1)
ans = 'uint8'
  2 Comments
Peter Perkins
Peter Perkins on 17 Jul 2023
Stephen23 should get extra extra reputation point for this one!
"curly-brace indexing can be used to index into some of the table rows"
vs.
"dot indexing to assign to a table basically reassigns the entire variable"
I will add that t.(expression) only extracts ONE variable at a time, while t{:,expression} can extract multiple vars as one array, if expression is like 1:3 or ["a" "b" "c"]. Check out https://www.mathworks.com/help/matlab/matlab_prog/computations-with-numeric-data-in-table-or-timetable.html for more about this, with the caveat that for R2023a and later, you should also read https://www.mathworks.com/help/matlab/matlab_prog/direct-calculations-on-tables-and-timetables.html and a couple of its siblings.

Sign in to comment.

More Answers (1)

Matt J
Matt J on 9 Jun 2023
Edited: Matt J on 9 Jun 2023
In Matlab, there are circumstances in which indexed assignments will convert the type of the right hand side in order to be consistent with the rest of the data in the variable. Leaving aside tables for a moment, we can see this with simple double vectors, e.g.,
x=1:5; class(x)
ans = 'double'
x(1)=uint8(7), class(x)
x = 1×5
7 2 3 4 5
ans = 'double'
The vector x is still of class double because when I assign uint8(7) to x(1), the operation automatically converts uint8(7) to double(7) because the rest of the x(i) are already doubles, and this conversion needs to be done so that they are left unaffected by the assignment.
You are seeing something analogous in the case of tables. It is the same as if you had done,
load patients
data = table(Gender,Age);
for i=1:height(data)
data{i,2} = uint8(data{i,2});
end
In each pass through the loop uint8(data{i,2}) will be converted to double for the same reason as with the double vector x. The syntax data.(2) =___ is a special syntax designed for tables which allows you to rebuild the entire table column from scratch.
  2 Comments
Matt J
Matt J on 9 Jun 2023
Note that not all indexed assignments result in a conversion, because some variables are treated as containers for other variables. Consider the case of a cell array,
C={[1;2;3],'dog'}
C = 1×2 cell array
{3×1 double} {'dog'}
This indexed assignment will completely overwrite C{1} with data of a new class,
C{1}=uint8(C{1})
C = 1×2 cell array
{3×1 uint8} {'dog'}
whereas this will leave it untouched,
C{1}(:)=double(C{1}(:))
C = 1×2 cell array
{3×1 uint8} {'dog'}

Sign in to comment.

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!