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
0 个评论
采纳的回答
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
0 个评论
更多回答(1 个)
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 个评论
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 Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!