Determining whether a table's CustomProperties are 'table' or 'variable'

7 views (last 30 days)
MATLAB's table data type can include user defined custom properties that either apply to the table or to the variables.
dat = table(ones(10,1), randi([0,3],10,1));
dat = addprop(dat,{'meta','numel'},{'table','variable'});
dat.Properties.CustomProperties.meta = "I'm an example per table property.";
dat.Properties.CustomProperties.numel = varfun(@(x)numel(unique(x)),dat,'OutputFormat','uniform');
disp(dat.Properties)
TableProperties with properties: Description: '' UserData: [] DimensionNames: {'Row' 'Variables'} VariableNames: {'Var1' 'Var2'} VariableDescriptions: {} VariableUnits: {} VariableContinuity: [] RowNames: {} Custom Properties (access using t.Properties.CustomProperties.<name>): meta: "I'm an example per table property." numel: [1 3]
I have a custom summary tool that works on tables' variables, but also needs to know if any CustomProperties are per table or per variable. Is there a way to determine if the custom table property applies to the table or the variable(s)?
Workarounds
Check Size: I crudely check if the custom property's size is the same as the table's Properties' VariableNames field. This fails on table properties when the table has a single named data column. I would like to avoid hand-coding explicit CustomProperties variables to check.
fs = fieldnames(dat.Properties.CustomProperties);
m = table( ...
'Size', [numel(fs),1], ...
'VariableTypes',{'logical'}, ...
'VariableNames',{'isVarProp'}, ...
'RowNames', fs );
sz = size(dat.Properties.VariableNames);
for f = string(reshape(fs,1,[]))
m.isVarProp(f) = isequal(sz,size(dat.Properties.CustomProperties.(f)));
end
perVarProps: I found that CustomProperties is a mixin type, and there are perTableProps and perVarProps. Each field of perVarProps is a variable property from addprop. I just need the field names of that property. I can use struct to expose those per*Props, with a warning as a penalty. I can decide how to suppress the warning, i.e. create a restore cleanup task in the beginning of the custom summary tool, or before the custom summary tool if calling en masse (which I am on 10K~100K tables).
%{
# This is a workaround to avoid printing the warning message.
warningStructOnObjectState = warning('off','MATLAB:structOnObject');
restoreStructOnObjectWarns = onCleanup(@()warning(warningStructOnObjectState));
%}
disp(struct(dat.Properties.CustomProperties))
Warning: Calling STRUCT on an object prevents the object from hiding its implementation details and should thus be avoided. Use DISP or DISPLAY to see the visible public details of an object. See 'help struct' for more information.
perTableProps: [1×1 struct] perVarProps: [1×1 struct] version: 1

Answers (1)

Dave B
Dave B on 8 Oct 2021
One strategy here would be to use the output of summary, which returns a struct with a CustomProperties field for each Var, and that has the 'variable' type properties. It's somewhat imperfect as it requires you to have a variable, but maybe an improvement on the other workarounds?
dat = table(ones(10,1), randi([0,3],10,1));
dat = addprop(dat,{'meta','numel'},{'table','variable'});
dat.Properties.CustomProperties.meta = "I'm an example per table property.";
dat.Properties.CustomProperties.numel = varfun(@(x)numel(unique(x)),dat,'OutputFormat','uniform');
disp(dat.Properties)
TableProperties with properties: Description: '' UserData: [] DimensionNames: {'Row' 'Variables'} VariableNames: {'Var1' 'Var2'} VariableDescriptions: {} VariableUnits: {} VariableContinuity: [] RowNames: {} Custom Properties (access using t.Properties.CustomProperties.<name>): meta: "I'm an example per table property." numel: [1 4]
datsum = summary(dat);
custompervar = {};
if ~isempty(datsum)
vars = fieldnames(datsum);
if isfield(datsum.(vars{1}),'CustomProperties')
custompervar = fieldnames(datsum.(vars{1}).CustomProperties);
end
end
custompertable = setdiff(fieldnames(dat.Properties.CustomProperties),custompervar);
disp(custompervar)
{'numel'}
disp(custompertable)
{'meta'}

Categories

Find more on Tables in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!