Is There an Inconsistency with How Unassigned Values are Filled After an Assignment into a Table?

36 次查看(过去 30 天)
Create an empty table
T = table;
Assign a string value to the third row a new variable
T.string(3) = "abc"
T = 3×1 table
string _________ <missing> <missing> "abc"
The values in rows 1 and 2 are assigned as <missing>. Same basic behavior with a datetime
T.date(3) = datetime
T = 3×2 table
string date _________ ____________________ <missing> NaT <missing> NaT "abc" 10-Dec-2025 17:24:51
Do the same thing with a numeric
T.number(3) = 5
T = 3×3 table
string date number _________ ____________________ ______ <missing> NaT 0 <missing> NaT 0 "abc" 10-Dec-2025 17:24:51 5
I was expecting the unassigned number values to be filled with the @doc:missing value that corresponds to a double, which is NaN. Alas, it fills in with zeros, so now we can't go back afterwards and find which were the missing values that were automagically filled
ismissing(T)
ans = 3×3 logical array
1 1 0 1 1 0 0 0 0
I realize that filling with NaN would be problematic if NaN is a valid entry that could be assigned to an element of T.number, but it seems like filling with NaN would be better than zero in the majority of cases and that it would be more consistent with the other data types.
Or perhaps it's intended that T.num(3) = 5 should fill with zeros to consistent with how Matlab initizalizes unassigned elements of vectors?
x(3) = 5
x = 1×3
0 0 5
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
I think that's a typical use case, but I'm not sure that same paradigm follows for data arranged in a table.

采纳的回答

dpb
dpb 2025-12-10,17:59
编辑:dpb 2025-12-10,18:12
It's identically the same as the assignment to the end of a numeric vector or array.
x(3)=5
x = 1×3
0 0 5
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Using the syntax
clear x % just to empty workspace
N=3;
X(N,N)=0
X = 3×3
0 0 0 0 0 0 0 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
is a shorthand for zeros(N) and can be faster for large N.
To keep the NaN, you would have to explicitly create the array and set the desired value(s) in it, not rely on the automatic memory allocation.
The expression of
t.NewVariable(N)=value;
internally first creates the vector to assign first even though it is transient.
  2 个评论
Paul
Paul 2025-12-10,18:15
编辑:Paul 2025-12-10,18:50
Yes, I realize that the end result is functionally the same as initializing the end of numeric array; I'm questioning if that's really the desired behavior.
Regarding:
"The expression of
t.NewVariable(N)=value;
internally first creates the vector to assign [t]o first even though it is transient."
That might be the case for the example shown when a new table variable is created, but I wonder if that's what happens when appending to an existing variable.
Suppose we have the following:
T = table;
T.a(1e5) = 5; T.b(1e5) = 10;
T.b(1e5+2) = pi;
Warning: The assignment added rows to the table, but did not assign values to all of the table's existing variables. Those variables are extended with rows containing default values.
Are you claiming that the assignment to T.b(1e5+2) causes all 1e5 old elements of T.a and T.b to be copied, then appended with two elements, then stuffed back into T (perhaps with another copy)?
dpb
dpb 2025-12-10,18:35
编辑:dpb 2025-12-10,20:08
No, but that's an entirely different scenario than the case of the initial question.
I've not tried to ferret out the actual code in the scenario of appending a variable although I do have quite a bit of code that does precisely that although I generally write an "append" function to fill a blank row of the table with values of the default type that can populate with those known at the time of adding the record(s). I've submitted an enhancement request to have a builtin function/method for the table class that does that automagically instead of having to build the datatypes to match. I've also requested that the assignment of a constant be allowed for appending instead of the error of "all entries must be of same height" when the appended data may be variable for one but a constant for others to avoid the need for the repmat construction.
I'm sure internally the JIT compiler is sufficiently clever to know it only has to build and append the 2xNvariables array to append in the above case
As far as whether it is "desired" behavior will, I think, depend entirely upon the desiree and the particular application.
Given that it is and has always been the behavior; there's zero chance it will be changing; it would break far too much existing code (and probably even internal) to even consider.

请先登录,再进行评论。

更多回答(1 个)

Walter Roberson
Walter Roberson 2025-12-10,18:04
T(3) = "abc"
T = 1×3 string array
<missing> <missing> "abc"
U(3) = datetime('now')
U = 1×3 datetime array
NaT NaT 10-Dec-2025 18:04:23
x(3) = 5
x = 1×3
0 0 5
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
So the filling is consistent with arrays.

类别

Help CenterFile Exchange 中查找有关 Tables 的更多信息

产品


版本

R2025b

Community Treasure Hunt

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

Start Hunting!

Translated by