How can I check if a variable exists within another variable?

11 ビュー (過去 30 日間)
Raymond Mo
Raymond Mo 2019 年 7 月 8 日
コメント済み: Raymond Mo 2019 年 7 月 8 日
I have a temp variable that changes depending on the type of data being processed. I want to check if that temp variable contains a field called sig or a field called SI. I've written the code below:
if exist('temp.sig','var') ~= 0 %checks if line or map
N = normalize(temp.sig);
image(app.UIAxes,N);
handles.isLine = true;
%display line
elseif exist('temp.SI','var') ~= 0
N = normalize(temp.SI(1,:,:));
image(app.UIAxes,N)
else
end
The issue is that whenever I run this it immediately evaluates both statements to false, even though one or the other should be true. It seems as if the exist statement doesn't check within the temp variable for SI or sig, it just looks for the names in the workspace. I don't know what the issue is or how to fix it.

採用された回答

Walter Roberson
Walter Roberson 2019 年 7 月 8 日
if isfield(temp, 'sig')
N = normalize(temp.sig);
image(app.UIAxes,N);
handles.isLine = true;
elseif isfield(temp, 'SI')
N = normalize(temp.SI(1,:,:));
image(app.UIAxes,N)
end
  1 件のコメント
Raymond Mo
Raymond Mo 2019 年 7 月 8 日
Thanks for the quick answer! It works :)

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

その他の回答 (1 件)

John D'Errico
John D'Errico 2019 年 7 月 8 日
It fails, because exist does not apply to fields of a strcture.
help isfield
isfield True if field is in structure array.

カテゴリ

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

製品

Community Treasure Hunt

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

Start Hunting!

Translated by