Error using inputdlg() second time through same code

3 vues (au cours des 30 derniers jours)
Gavin
Gavin le 19 Sep 2024
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 commentaires
Gavin
Gavin le 19 Sep 2024
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 le 19 Sep 2024
Modifié(e) : Stephen23 le 19 Sep 2024
"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"

Connectez-vous pour commenter.

Réponse acceptée

Walter Roberson
Walter Roberson le 19 Sep 2024
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 commentaires
Gavin
Gavin le 3 Oct 2024
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 le 3 Oct 2024
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.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Produits


Version

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by