필터 지우기
필터 지우기

Asking a question with specific format

조회 수: 1 (최근 30일)
Emma Sellers
Emma Sellers 2019년 11월 29일
편집: Image Analyst 2019년 11월 29일
I need to ask a question that is formatted like this:
*As a dialog box:
Enter the resistance between node __ and node ___ (where the node number is changing each time the question is asked):
If parallel enter resistance 1:
Resistance 2:
I need all of this as one dialog box, but split up becuase I need to detemine if the user leaves certain boxes empty in the question.
I have tried several options but have been unable to split into different elements of a cell and have a changing variable in the same dialog box...
Thank you in advance!!
  댓글 수: 6
Rik
Rik 2019년 11월 29일
It is probably easier to build a GUI yourself instead of hacking inputdlg. That is why I asked for a visual example.
Image Analyst
Image Analyst 2019년 11월 29일
A custom user interface could certainly look nicer, but it's not hard to use inputdlg(). I gave a snippet below that is easily adapted and fairly robust. It's practically done except for using sprintf() to write the node numbers into the user prompt string.

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

답변 (1개)

Image Analyst
Image Analyst 2019년 11월 29일
편집: Image Analyst 2019년 11월 29일
See this snippet and adapt as needed:
% Define default values:
resistances = [1, 2, 3];
for k = 1 : length(resistances)
defaultValue{k} = num2str(resistances(k));
end
% Ask user for two floating point numbers.
titleBar = 'Enter Resistance';
userPrompt = {'Enter the resistance between node __ and node ___', 'If parallel, Enter resistance 1 : ', 'If parallel, Enter resistance 2: '};
caUserInput = inputdlg(userPrompt, titleBar, 1, defaultValue);
if isempty(caUserInput),return,end % Bail out if they clicked Cancel.
% Convert to floating point from string.
resistance1 = str2double(caUserInput{1})
resistance2 = str2double(caUserInput{2})
resistance3 = str2double(caUserInput{3})
% Check resistance1 for validity.
if isnan(resistance1)
% 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.
resistance1 = str2double(defaultValue{1});
message = sprintf('I said it had to be a number.\nTry replacing the user.\nI will use %.2f and continue.', resistance1);
uiwait(warndlg(message));
end
% Do the same for resistance2 - check for a valid number.
% Check resistance2 for validity.
if isnan(resistance2)
% 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.
resistance2 = str2double(defaultValue{2});
message = sprintf('I said it had to be a number.\nTry replacing the user.\nI will use %.2f and continue.', resistance2);
uiwait(warndlg(message));
end
% Do the same for resistance3 - check for a valid number.
% Check resistance3 for validity.
if isnan(resistance3)
% 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.
resistance3 = str2double(defaultValue{2});
message = sprintf('I said it had to be a number.\nTry replacing the user.\nI will use %.2f and continue.', resistance3);
uiwait(warndlg(message));
end

카테고리

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