Asking a question with specific format

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

I don't fully understand what you mean. Can you make an example in paint of what you mean?
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
The question there that is titled "Resistances"
But the dialog box comes out all wrong and does not split it up the way I want it.
It is probably easier to build a GUI yourself instead of hacking inputdlg. That is why I asked for a visual example.
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

Asked:

on 29 Nov 2019

Edited:

on 29 Nov 2019

Community Treasure Hunt

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

Start Hunting!