"One condition I need is that if the row has more columns, I want the table to be amended by "NaN" or otherwise empty columns, such that ismember always shows 0"
In that case, it is simpler to compare the number of columns and not bother calling ismember at all. In fact, ismember requires both tables to have exactly the same variable names, so you could just compare both set of variable names:
if ~isempty(setxor(table1.Properties.VariableNames, table2.Properties.VariableNames))
result = zeros(height(table1), 1);
else
result = ismember(table1, table2);
end
Or you could just call ismember and trap the error that will be raised when the number of variables or their names don't match
try
result = ismember(table1, table2);
catch
result = zeros(height(table1), 1);
end
" I wonder what sort of default element I could use to set up missing data in a table, given that NaN is not implemented for strings"
NaN is a numeric value. It can be used to indicate missing numbers but it does not make any sense for strings. If a variable is a string type, filling it with NaN is a very bad idea as the variable is then a mix of strings and numbers.
Matlab has a specific missing indicator for strings (<missing>), as long as you do mean string and not char array. For char array, the only thing you can use is an empty char array '' (which is indistiguishable from an empty char array unfortunately). For the types that support it you can use the missing function to generate an array of missing values. This gets converted to the proper missing indicator (NaN, NaT, <undefined>, <missing>) depending on the type.
Best Answer