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

2 visualizaciones (últimos 30 días)
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)?

Respuestas (1)

Bjorn Gustavsson
Bjorn Gustavsson el 26 de Jun. de 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 comentarios
Walter Roberson
Walter Roberson el 26 de Jun. de 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 el 26 de Jun. de 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?

Iniciar sesión para comentar.

Categorías

Más información sobre Structures en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by