Error using inputdlg() second time through same code

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 Comments

Other things I tried and it still doesn't work:
answer = {""}; % get around MatLab 2023b bug?
answer = inputdlg("Enter ...
So, in real program str is set to a Value of a text edit field (in case that matters)
str = app.Name.Value;
So I changing str in case it's actually an input error, not output error:
str = char(app.Name.Value);
Same error every time.
Inspite of claims to the contrary, it turns out it does matter if you do
str = "JUNK"
vs
str = 'JUNK'
>> str = 'JUNK'
str =
'JUNK'
>> class(str)
ans =
'char'
>> str = "JUNK"
str =
"JUNK"
>> class(str)
ans =
'string'
Fixed it with
str = string(app.Name.Value);
Suggestions to MatLab developers:
  1. If you are going to be so picky about types then say so don't pretend it doesn't matter. (' ' vs " ")
  2. If you are going to throw an error then identify it correctly so we don't waste our time on red herrings: Input error not output error
Another example where MatLab is sloppy and strict at the same time:
K>> sel = uiconfirm(app.MouseOdor,"Change the Output Folder?", ...
"Save Data Folder",Options=['YES','NO'], ...
Icon="question",DefaultOption=2);
Error using uiconfirm
'DefaultOption' must be a character vector or a string scalar from the 'Options'
cell array or an index of the 'Options' cell array
K>> sel = uiconfirm(app.MouseOdor,"Change the Output Folder?", ...
"Save Data Folder",Options=["YES","NO"], ...
Icon="question",DefaultOption=2);
K>> sel
sel =
'NO'
SO answer is ' ' type but my Options list have to be " "? Is this a new thing that was doumented somewhere?
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
"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"

Sign in to comment.

 Accepted Answer

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 Comments

My original question about inputdlg() got lost in the answers about uiconfirm()
The issue is that inputdlg takes str as input but gives you back a cell array. First time through, my saved answer has become a cell array I guess. Second time it no longer thinks answer is a cell array because of something else I did with answer, a local variable? I recall that it was a new call to the function so answer should have been a clean new var again. Maybe I was looping with in the function so answer kept the wrong type? Why not just change it back to the type it wants like it does every other time?
Error using inputdlg
Default Answer must be a cell array of character vectors.
MATLAB isn't strongly typed for MY variables, changing them with out telling me, but really picky about what it gets put to it's routines. And in this case won't change my "answer" to the type it wants.
>> str = "JUNK";
>> answer = inputdlg("Enter a Mouse Name","Mouse Name", [1 30],str)
answer =
1×1 cell array
{'JUNK'}
The solution was:
answer = inputdlg("Enter a Name","New Name", ...
[1 30],str);
if ~isempty(answer) % Don't change if canceled
app.Name.Value = answer{1}; % Pull the reply out of it's cell
end
Wonderfully consistant MATLAB for calling conventions with strings and chars and cells. Not even matching input format with the output! Help has so much verbiage about unnecessary stuff that it's hard sometimes to notice the little things like ' ' and " " especially when MATLAB says (sometimes) that it doesn't care and you can use either.
RE uiconfirm - from help example
selection = uiconfirm(fig,msg,title, ...
"Options",["Overwrite","Save as new","Cancel"], ...
"DefaultOption",2,"CancelOption",3);
The whole thing about using cells really isn't helpful, a string array of options is clearer for uiconfirm
This is another one of those gottcha's where ["YES","NO"] is different from ['YES','NO'] with no mLint help
It should be obvious even to lint that I don''t want a result of YESNO in the middle of a options= setting
Unaccepting this answer so I can get a reply from @Walter Roberson
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?
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.

Sign in to comment.

More Answers (0)

Products

Release

R2023b

Community Treasure Hunt

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

Start Hunting!