How to check for a custom property in a table

12 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 (3)

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 17 Jun 2022
Hmm, interesting. I wonder what I did wrong as I couldn't get it to work. I will check the command history when I get back to work and accept the answer, thanks!
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.

Boris Blagov
Boris Blagov on 8 May 2023
To revive my old threat for people that might be looking for this in the future:
Be careful on releases earlier than 2020b (which I had the option to test this on)!
isprop not only doesn't work as intended on Matlab 2019b but it also doesn't tell you that it doesn't. It simply defaults to 0.
If you have an if statement that checks for the field, the statement would always default to 0 as if the property is not there. At least, if you try to add it (while it is there) you will get an error, which is a safeguard against the code overwriting fields that it doesn't find due to that behaviour. But still you might have your program changing fields without you knowing.

Categories

Find more on Function Creation in Help Center and File Exchange

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!