How to check for a custom property in a table

5 views (last 30 days)
Hi,
is there an easy way to check if a Custom Property in a table has been set, i.e. if it exists?
I know I can pass a structure to UserData in a table with a series of fields and then use isfield to check if a field exists. I cannot find a similar way to check for properties. There is a command isprop but it either doesn't work for this or I am using it wrong. Also T.Properties.Customproperties is not a structure on which isfield can operate, even thought it uses the dot operator.
Here is a MWE
t = array2table(rand(10,3));
t = addprop(t,'LineWidth','variable');
t.Properties.CustomProperties
ans =
CustomProperties with properties: LineWidth: []
I want to check if LineWidth or DisplayName exist. One should give me 1, the other 0.
Thanks!

Answers (2)

Voss
Voss on 17 Jun 2022
Edited: Voss on 17 Jun 2022
isprop should work:
t = array2table(rand(10,3));
isprop(t.Properties.CustomProperties,'LineWidth')
ans = logical
0
isprop(t.Properties.CustomProperties,'DisplayName')
ans = logical
0
isprop(t,'LineWidth')
ans = logical
0
isprop(t,'DisplayName')
ans = logical
0
% add LineWidth property
t = addprop(t,'LineWidth','variable');
isprop(t.Properties.CustomProperties,'LineWidth')
ans = logical
1
isprop(t.Properties.CustomProperties,'DisplayName')
ans = logical
0
isprop(t,'LineWidth')
ans = logical
1
isprop(t,'DisplayName')
ans = logical
0
  2 Comments
Boris Blagov
Boris Blagov on 22 Jun 2022
Okay, so I managed to see the code in my office and that one still doesn't work. I have matlab 2019b. Here in the editor works but not at my office.
I guess that means it did not work in 2019b, am I right?
nbase = 6;
tab_base = array2table(zeros(0,nbase));
tab_base = addprop(tab_base,'LineWidth','variable');
tab_base = addprop(tab_base,'LineStyle','variable');
tab_base = addprop(tab_base,'Marker','variable');
% Defaults per variable
tab_base.Properties.CustomProperties.LineWidth = repmat(2,[1 nbase]);
tab_base.Properties.CustomProperties.LineStyle = repmat({'-'},[1,nbase]);
tab_base.Properties.CustomProperties.Marker = repmat({'none'},[1,nbase]);
test = isprop(tab_base.Properties.CustomProperties,'LineWidth')
test = logical
1
Here is my output and this is why I commented in my post that isprop didn't seem to work.
So my question remains, is there something to do for 2019b?

Sign in to comment.


Boris Blagov
Boris Blagov on 13 Jul 2022
So, after quite a lot of fumbling around I think it is not possible to use the table properties in the desired way in 2019b. For older releases passing a structure and checking with "isfield" seems to be more reliable.

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!