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
채택된 답변
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
댓글 수: 0
추가 답변 (1개)
Christian
2012년 5월 3일
댓글 수: 2
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?
참고 항목
카테고리
Help Center 및 File Exchange에서 Language Support에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!