MATLAB Answers

Avoid ''Dot indexing is not supported for variables of this type.'', exist(...) isn't working

5 views (last 30 days)
Gabriel Bischof
Gabriel Bischof on 26 Jun 2020
Commented: Bjorn Gustavsson on 26 Jun 2020
I want to check if the variable is valide.
I want to do this: stop(handle.UserData.sectionPowerSuply.tCyclic);
The Problem is that when there is an error while building the sectionPowerSuply this section dont exist.
is there a way to do something like this: exists(handle.UserData.sectionPowerSuply.tCyclic.Running)?

  0 Comments

Sign in to comment.

Answers (1)

Bjorn Gustavsson
Bjorn Gustavsson on 26 Jun 2020
If you want to check if a struct has a field your're interested in you should check with isfield, check the help and documentation for that function. With your hierarcical struct it will be a couple of calls to be sure that the handle has a field 'UserData' first then the next field down, but you'll get there.
HTH

  4 Comments

Show 1 older comment
Gabriel Bischof
Gabriel Bischof on 26 Jun 2020
thanks, i have found a Solution, this works for me:
exist('handle.UserData.sectionPowerSuply.tCyclic.Running','var')
Walter Roberson
Walter Roberson on 26 Jun 2020
No, when you give a structure reference, MATLAB will always say it does not exist as a variable. You need to test with isfield()
Bjorn Gustavsson
Bjorn Gustavsson on 26 Jun 2020
Gabriel, that's good. However, when I test this in matlab-2020a:
qwe.asd = 1;
exist('qwe.zxc','var')
%
% ans =
%
% 0
% This is what you'd want
exist('qwe.asd','var')
%
% ans =
%
% 0
% This I'd guess you would want to be true?
So your solution might not be robust across matlab-versions, so maybe make a note to future you?

Sign in to comment.