Error using inputdlg() second time through same code

3 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by