Comma-separated assignment to a table variable
Mostra commenti meno recenti
The code below tests assignment by comma-separated expansion to a cell array in 3 different scenarios. In the specific case when the cell array X is the variable of a table, it fails. Why is that?
X=cell(1,3);
S.X=X;
T=table(X);
[X{:}]=deal(1,2,3) %Case (1) : works fine
[S.X{:}]=deal(1,2,3) %Case (2) : works fine
[T.X{:}]=deal(1,2,3) %Case (3) : fails
13 Commenti
Stephen23
il 19 Giu 2026 alle 20:00
Table overloads both subsref and subsasgn. Its indexing is nothing like any other class.
Why does the second call to deal result in three copies of the same displayed output?
X = cell(1,3);
T = table(X);
[X{:}] = deal(1,2,3)
[T.X{1},T.X{2},T.X{3}] = deal(1,2,3)
Matt J
il 19 Giu 2026 alle 21:42
Matt J
il 20 Giu 2026 alle 12:57
dpb
il 20 Giu 2026 alle 13:47
I note there that the table/timetable are notably missing the "Use in comma separated lists" line item in the "Intended Use" column....guess it also shows how long it's been since I did more than look up individual functions in the doc -- I had forgotten about that overview page existing.
While I'd never attempted the addressing to a table in that fashion and wouldn't have thought of doing so in all likelihood anyway, I'll confess I don't know enough about the internals for it to be apparent to me why it couldn't be done; I would presume adding the additional complexity on top of existing logic could be a performance issue in adding yet another layer of complexity.
AFAICT, numArgumentsFromSubscript alway returns 1 for a table for assignment, at least for the various cases that I tried, which is why deal fails for the example at hand. But that also means that deal works for assigning to a scalar cell in the table variable
X = cell(1,1);
T = table(X);
[T.X{:}] = deal(pi)
or to one element of vector cell
X = cell(1,3);
T = table(X)
[T.X{2}] = deal(pi)
And deal can expand the table variable
[T.X{4}] = deal(8)
[T.X{2,3}] = deal(100)
Peter Perkins
circa 5 ore fa
Paul, I can't tell what you are trying to demonstrate. Putting square brackets around things like X{2} and then using deal makes no sense to me. T.X{2} is one thing, i.e. "the contents of" the 1x3 cell array t.X. You don't need square brackets or deal for this any more than you need them for X{2} = pi.
>> X = cell(1,3);
>> T = table(X)
T =
table
X
____________________________________________
{0×0 double} {0×0 double} {0×0 double}
>> T.X{2} = pi
T =
table
X
__________________________________________
{0×0 double} {[3.1416]} {0×0 double}
Even assigning two elements:
>> T.X(1,2:3) = {100}
T =
table
X
__________________________________
{0×0 double} {[100]} {[100]}
I will also say that
>> table(cell(3,1))
ans =
3×1 table
Var1
____________
{0×0 double}
{0×0 double}
{0×0 double}
is probably what you want, not
>> table(cell(1,3))
ans =
table
Var1
____________________________________________
{0×0 double} {0×0 double} {0×0 double}
but I can't say for sure.
Paul
circa 4 ore fa
I was just trying to show that deal works when assigning scalar to scalar when using the general form
[CSL] = deal(CSL)
even though deal doesn't work for other cases as shown. That seemed like an inconsistency to me, even if one would never need to use deal for those scalar to scalar assignments.
I used table(cell(1,3)) because that was the example used in the Question that started this thread.
Risposta accettata
Più risposte (1)
The table is a one-row cell array in one vairable so it can be assigned directly as a cell can be...
X=cell(1,3);
T=table(X)
T.X={1 2 3}
No need for deal() here as the dot-X notation returns the cell array in its entirety. Of course, in a table cell, it can contain anything any other cell can
T.X(1:2)={1 2};
T.X(3)={'Fred Flintstone'}
T.X
2 Commenti
Peter Perkins
circa 5 ore fa
Ah, I see that dpb has beat me to it. I will just add: If you find yourself relying on CSLs and/or deal anywhere in MATLAB, there is almost certainly a better way to do it.
In this case, the better way is, leave the cell array (whether it be X or T.X) as a cell array. Don't CSL expand it. I'm skeptical (but of course might be wrong) that deal(1,2,3) represents a real use case where there are three separate things in separate workspacce vars. But even if it is, just wrap them in braces and assign to the cell array:
>> X = cell(3,1);
>> T = table(X)
T =
3×1 table
X
____________
{0×0 double}
{0×0 double}
{0×0 double}
>> T.X = {1;2;3}
T =
3×1 table
X
_____
{[1]}
{[2]}
{[3]}
>> T.X(:) = {11;22;33}
T =
3×1 table
X
_____
{[11]}
{[22]}
{[33]}
>> T.X(2:3) = {222;333}
T =
3×1 table
X
______
{[ 11]}
{[222]}
{[333]}
Categorie
Scopri di più su Tables in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!