How would I check to see if a struct field exists OR is empty?

242 ビュー (過去 30 日間)
Darren Wethington
Darren Wethington 2019 年 2 月 28 日
回答済み: Andrei Cimponeriu 2024 年 4 月 19 日 11:21
I'd like to program a conditional statement that checks if an optional events function for an ODE was triggered. This would look something like:
if ~isfield(sol,'xe') || isempty(sol.xe)
fprintf('Event Not Triggered\n')
else
fprintf('Event Triggered\n')
end
However, this fails if sol.xe doesn't exist because no events function was specified. In the interest of shorter/cleaner code, I'd like to avoid this solution:
if isfield(sol,'xe')
if isempty(sol.xe)
fprintf('Event Not Triggered\n')
else
fprintf('Event Triggered\n')
end
else
fprintf('Event Not Triggered\n')
end
Is there a better way to do this?
  3 件のコメント
Darren Wethington
Darren Wethington 2019 年 2 月 28 日
Many apologies, I wrote this up and did not test it. Comparing it to my actual code, I had something that mimicked this instead:
if isempty(sol.xe) || ~isfield(sol,'xe')
fprintf('Event Not Triggered\n')
else
fprintf('Event Triggered\n')
end
This generates an error when tested, so it seems that ordering matters here; the second condition is only checked if the first is not met.
Thank you for helping me to understand my code and Matlab better, and I apologize for the inconvenience. Thanks to @Walter Robinson as well.
Walter Roberson
Walter Roberson 2019 年 2 月 28 日
編集済み: Walter Roberson 2019 年 2 月 28 日
Conditions are evaluated left to right except when there are (), in which case everything before the () is evaluated and then the inside of the () is evaluated as a group.
When the || or && operators are used, the values on the two sides must be scalar (even empty is not permitted), and the calculation within that group stops as soon as it figures out the answer-- so
if A||B
declares success if A is true, without executing B, but continuing on to B if A is false; and
if A&&B
declares failure if A is false, without executing B, but continuing on to B if A is true.
This is not the case for the & or | operators: they execute both sides anyhow.

サインインしてコメントする。

採用された回答

Walter Roberson
Walter Roberson 2019 年 2 月 28 日
You might find this clearer:
if isfield(sol,'xe') && ~isempty(sol.xe)
fprintf('Event Triggered\n')
else
fprintf('Event Not Triggered\n')
end
However, your existing code is fine. In your existing code, if there is no xe field then the ~isfield is true and the || short-circuits and you get the Not Triggered. If there is an xe field then the ~isfield is false but you are in a || so the second test is performed which is about emptiness. You can only reach the Triggered message if there is an xe field and it is not empty, which is what you want.

その他の回答 (1 件)

Andrei Cimponeriu
Andrei Cimponeriu 2024 年 4 月 19 日 11:21
Hi,
For a structure named TT having one of the fields called FF, you could try this:
if ismember('FF',fieldnames(TT)) ...
Best regards,
Andrei

カテゴリ

Help Center および File ExchangeVariables についてさらに検索

製品


リリース

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by