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"
The values in rows 1 and 2 are assigned as <missing>. Same basic behavior with a datetime
T.date(3) = datetime
Do the same thing with a numeric
T.number(3) = 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)
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
I think that's a typical use case, but I'm not sure that same paradigm follows for data arranged in a table.
0 个评论
采纳的回答
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
Using the syntax
clear x % just to empty workspace
N=3;
X(N,N)=0
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 个评论
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
2025-12-10,18:04
T(3) = "abc"
U(3) = datetime('now')
x(3) = 5
So the filling is consistent with arrays.
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!