필터 지우기
필터 지우기

Asking multiple questions to user

조회 수: 4 (최근 30일)
Emma Sellers
Emma Sellers 2019년 11월 22일
댓글: Image Analyst 2019년 11월 22일
Hi, Can someone tell me how to ask the user multiple questions while also have one question that changes variables? I want the question below to be an array with 3 different questions.. Is this possible? Thanks!
while B < nodes
B = B+1;
Resistances =inputdlg(['Enter resistance between ',num2str(A),' and ',num2str(B),' or click Ok for infinite resistance.','If resistors in parallel share nodes, Resistance 1:','Resistance 2:']); %changed
if isempty(Resistances(1,2))
if strcmp(Resistances, {''}) % changed... when a user clicks "okay", "Resistances" would have an empty cell
ResistanceMat(A,B) = inf % added... the corresponding element in the matrix is converted to inf, without this it converted to NaN which is not usable in the equation
ResistanceMat(B,A) = inf % added
else
ResistanceMat(A,B) = str2double(Resistances)
ResistanceMat(B,A) = str2double(Resistances)
end

답변 (1개)

Image Analyst
Image Analyst 2019년 11월 22일
Here's a snippet that asks 2 questions. Make the obvioius modifications to change it to three questions.
% 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
  댓글 수: 2
Emma Sellers
Emma Sellers 2019년 11월 22일
This does not work when your asking a question with a changing variable in it.
Image Analyst
Image Analyst 2019년 11월 22일
Why not? I'm not sure what "a changing variable" means. You can certainly use sprintf() to write the current value of a variable into the userPrompt strings. AND, the user is certainly able to change those default variables in the dialog box. AND, you can even take the user's entries and make changes to those. So exactly what does "does not work" mean to you?

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

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by