How to display a variable value in a text box?

7 次查看(过去 30 天)
Hello,
I have an inputdlg function to allow the user to introduce any value. How could I display the value of a variable in the "default" field of that function, which is shown in the text box?
Thank you.
  3 个评论
Christian
Christian 2012-5-3
If you take a look at the inputdlg function, you will see that one of the parameters you can define is "default", which displays its content in the input box which can be later modified by the user.
Thank you.

请先登录,再进行评论。

采纳的回答

Image Analyst
Image Analyst 2012-5-3
% Ask user for a number.
defaultValue = 45;
titleBar = 'Enter a value';
userPrompt = 'Enter the integer';
caUserInput = inputdlg(userPrompt, userPrompt, 1, {num2str(defaultValue)});
if isempty(caUserInput),return,end; % Bail out if they clicked Cancel.
integerValue = round(str2num(cell2mat(caUserInput)));
% Check for a valid integer.
if isnan(integerValue)
% They didn't enter a number.
% They clicked Cancel, or entered a character, symbols, or something else not allowed.
integerValue = defaultValue;
message = sprintf('I said it had to be an integer.\nI will use %d and continue.', integerValue);
uiwait(warndlg(message));
end

更多回答(1 个)

Christian
Christian 2012-5-3
Hi Image Analyst,
Excuse my stupidity, but, where do I have to place that code?
This is what I have right now:
param={' ... '):'};
name=' ... ';
lines=1;
default={' ... '};
output=inputdlg(param,name,lines,default);
The last line displays the text box with the parameters specified above, so I think I have to change the "default" value first.
Thank you for your answer.
  2 个评论
Image Analyst
Image Analyst 2012-5-3
Just copy and paste and run. You'll see the default value of 45 is displayed in the text box where the user is supposed to type their answer. Isn't that what you want? Your code is almost like mine except that some variables (param, name) don't have names that accurately describe what they are, and you just have dot dot dot instead of actual values like I do. Any reason why you can't use my code and just replace the default value of 45 and the prompt with ones suitable for your situation?
Christian
Christian 2012-5-3
Excuse me again, but didn't you change your answer like 20 minutes ago?
Anyway, my main problem was that I could not combine text and the value of a variable in the same text string as I was not using the curly brackets in the proper way. I called the default variable as you did and it works now.
Thank you.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Language Support 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by