How to check for a custom property in a table

조회 수: 14 (최근 30일)
Boris Blagov
Boris Blagov 2022년 6월 17일
답변: Boris Blagov 2023년 5월 8일
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!

답변 (3개)

Voss
Voss 2022년 6월 17일
편집: Voss 2022년 6월 17일
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
Boris Blagov
Boris Blagov 2022년 6월 17일
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 2022년 6월 22일
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?

댓글을 달려면 로그인하십시오.


Boris Blagov
Boris Blagov 2022년 7월 13일
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 2023년 5월 8일
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.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by