Error using inputdlg() second time through same code

3 ビュー (過去 30 日間)
Gavin
Gavin 2024 年 9 月 19 日
コメント済み: Walter Roberson 2024 年 10 月 3 日
My code works fine on the first pass but when I press the button a second time I get an error.
answer is a local variable so is undefined until it's used both times
str = "Default Name";
answer = inputdlg("Enter a Name","New Name", ...
[1 30],str);
Error using inputdlg
Default Answer must be a cell array of character vectors.
Break point at answer = to check
K>> answer
Unrecognized function or variable 'answer'.
What's the deal? Both times through answer doesn't exist until it's created/assigned a value by inputdlg
  3 件のコメント
Gavin
Gavin 2024 年 9 月 19 日
I couldn't find anything from Help Center so I asked Google a simple question
'' vs "" in MatLab
and got this
So apparently around 2019 MatLab started distinguishing between these things which is fine. Now just clean up the docs where it says it doesn't matter. I can't find that lesson at the moment but here's a good practice that maybe MatLab could follow internally
Stephen23
Stephen23 2024 年 9 月 19 日
編集済み: Stephen23 2024 年 9 月 19 日
"SO answer is ' ' type but my Options list have to be " "?"
No, it does not "have to be". The UICONFIRM documentation
states that the OPTIONS values may be either a cell array of character vectors or a string array. However, instead of providing the function with its documented input classes, you provided it with a single character vector:
['YES','NO'] % what you did: concatenate two character vectors into one character vector
ans = 'YESNO'
Note that square brackets are a concatenation operator, not a "list" operator (which MATLAB does not have).
Although in this case UICONFIRM was gracious enough to accept your undocumented input, it is clear that what you provided it with was one option, not two (thus the error when you told it to use the nonexistent 2nd option).
In general, the best way to use functions is to follow their documentation:
{'YES','NO'} % cell array of character vectors
ans = 1x2 cell array
{'YES'} {'NO'}
["Yes","No"] % string array
ans = 1x2 string array
"Yes" "No"

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

採用された回答

Walter Roberson
Walter Roberson 2024 年 9 月 19 日
Options=['YES','NO']
['YES','NO'] is a request to horzcat('YES', 'NO') which gives the single character vector result 'YESNO' .
['YES','NO'] is not a request to produce two separate values, 'YES' and 'NO'
You should use
Options={'YES','NO'}
  3 件のコメント
Gavin
Gavin 2024 年 10 月 3 日
Well, that didn't work, I guess I didn't really need an answer from @Walter Roberson to
Another one of those gottcha's where ["YES","NO"] is different from ['YES','NO'] with no mLint help?
Walter Roberson
Walter Roberson 2024 年 10 月 3 日
I do not particularly expect an mlint warning in that circumstance. ['YES','NO'] is a valid character-building expression. As long as uiconfirm accepts a character vector in that position, mlint has no reason to complain.

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

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by