フィルターのクリア

Contradiction of variable existence

1 回表示 (過去 30 日間)
Valeriy
Valeriy 2024 年 2 月 15 日
編集済み: Voss 2024 年 2 月 15 日
Hello,
I met following situation. In workspace exist some string variable strF. I need to verify its existence and if it is not present, I should perform some actions. But I have following contradiction:
~exist(strF,"var") gives me logical 1, which means that strF don't exist. This is not correct.
But if I run command
exist strF
I have 1, that means that this variable exist. But when I try to realize this infomation by something like
TT = exist(strF) gives me 0!
How correctly determine existence of this variable?
  6 件のコメント
Valeriy
Valeriy 2024 年 2 月 15 日
@Voss name of the variable MUST BE suppied in quotes! Otherwise command's result is incorrect
Voss
Voss 2024 年 2 月 15 日
編集済み: Voss 2024 年 2 月 15 日
Yes, the name of the variable, not the value of the variable. Name implies it's in quotes, when using function syntax! Did you follow the example I ran through in this comment?

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

採用された回答

Voss
Voss 2024 年 2 月 15 日
Use:
~exist("strF","var")
  2 件のコメント
Voss
Voss 2024 年 2 月 15 日
You must supply exist with the name of the variable to check for.
This is what you were doing:
strF = "some_string";
% this checks for a variable called "some_string", which is the value of
% strF, but no variable called "some_string" exists
exist(strF,"var")
ans = 0
some_string = 99; % now "some_string" exists, so
exist(strF,"var") % this returns true (1)
ans = 1
As opposed to:
clear("strF") % clear strF
exist("strF","var") % strF doesn't exist now
ans = 0
strF = "some_string"; % now it does
exist("strF","var") % and exist shows that
ans = 1
some_string = 99; % and it doesn't matter about "some_string"
exist("strF","var") % because now exist is checking for a variable called "strF"
ans = 1
Voss
Voss 2024 年 2 月 15 日
編集済み: Voss 2024 年 2 月 15 日
And the reason that this works
exist strF
is because it is command syntax. See:

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeData Type Identification についてさらに検索

タグ

製品


リリース

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by