Asking a question with specific format

1 view (last 30 days)
Emma Sellers
Emma Sellers on 29 Nov 2019
Edited: Image Analyst on 29 Nov 2019
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 Comments
Rik
Rik on 29 Nov 2019
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 on 29 Nov 2019
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.

Sign in to comment.

Answers (1)

Image Analyst
Image Analyst on 29 Nov 2019
Edited: Image Analyst on 29 Nov 2019
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

Community Treasure Hunt

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

Start Hunting!