Avoid ''Dot indexing is not supported for variables of this type.'', exist(...) isn't working
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(1 个)
Bjorn Gustavsson
2020-6-26
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 个评论
Walter Roberson
2020-6-26
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
2020-6-26
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?
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Structures 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!