Asking a question with specific format

1 次查看(过去 30 天)
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