Determining whether a table's CustomProperties are 'table' or 'variable'
12 次查看(过去 30 天)
显示 更早的评论
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)
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))
0 个评论
回答(1 个)
Dave B
2021-10-8
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)
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)
disp(custompertable)
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!