How to read user input of multiple string separately in inputdlg

조회 수: 7 (최근 30일)
I want to enter user input using inputdlg,
if true
% prompt = {'Input the number of Criteria','Input short name of criteria'};
dlg_title = 'Alternative Evauation';
num_lines = 1;
defaultans = {'3','{Criteria1,Criteria2,Criteria3}'};
answer = inputdlg(prompt,dlg_title,num_lines,defaultans);
end
although I am able to read 'number of Criteria' as
n=str2num(answer{1})
but when I try to read the read the short name of criteria
as
str=answer{2}
then instead of reading str as 'Criteria1', 'Criteria2', 'Criteria3'separately, it reads as Criteria1Criteria2Criteria3,
Please tell me how can I handle the multiple string input separately.
Thanks

채택된 답변

Image Analyst
Image Analyst 2017년 4월 22일
Here's a snippet I post regularly:
% Ask user for two floating point numbers.
defaultValue = {'45.67', '78.91'};
titleBar = 'Enter a value';
userPrompt = {'Enter floating point number 1 : ', 'Enter floating point number 2: '};
caUserInput = inputdlg(userPrompt, titleBar, 1, defaultValue);
if isempty(caUserInput),return,end; % Bail out if they clicked Cancel.
% Convert to floating point from string.
usersValue1 = str2double(caUserInput{1})
usersValue2 = str2double(caUserInput{2})
% Check usersValue1 for validity.
if isnan(usersValue1)
% They didn't enter a number.
% They clicked Cancel, or entered a character, symbols, or something else not allowed.
% Convert the default from a string and stick that into usersValue1.
usersValue1 = str2double(defaultValue{1});
message = sprintf('I said it had to be a number.\nTry replacing the user.\nI will use %.2f and continue.', usersValue1);
uiwait(warndlg(message));
end
% Do the same for usersValue2
% Check usersValue2 for validity.
if isnan(usersValue2)
% They didn't enter a number.
% They clicked Cancel, or entered a character, symbols, or something else not allowed.
% Convert the default from a string and stick that into usersValue2.
usersValue2 = str2double(defaultValue{2});
message = sprintf('I said it had to be a number.\nTry replacing the user.\nI will use %.2f and continue.', usersValue2);
uiwait(warndlg(message));
end

추가 답변 (1개)

Star Strider
Star Strider 2017년 4월 22일
Try this instead:
prompt = {'Input the number of Criteria','Input short name of Criterion 1','Input short name of Criterion 2','Input short name of Criterion 3'};
dlg_title = 'Alternative Evauation';
num_lines = [1 20];
defaultans = {'3','Criterion 1','Criterion 2','Criterion 3'};
answer = inputdlg(prompt,dlg_title,num_lines,defaultans);
  댓글 수: 2
Sanjeev Goyal
Sanjeev Goyal 2017년 4월 22일
@Star Strider Thank you very much for your response, but there is no limit on inputting the number of criterion, it may be 5 or 7, depending upon the User, so accordingly name of criterion will increase, how to tackle that. Later on I want to concatenate all the input names of criterion into single variable.
Star Strider
Star Strider 2017년 4월 22일
My pleasure.
You will have to experiment on your own to get the inputdlg function to do what you want, if you want it to be adaptable. One way would be to set up the maximum number of criteria you believe would ever be needed, and test for the response being different from the default. Or ask first in another inputdlg for the number the user will provide, then dynamically design the second inputdlg to accommodate that number.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by