10/17/2010
Dialog Boxes
(Using Cell arrays)
1.
2.
3.
4.
Example
p Project
j
Example GUI
inputdlg()
help
1
Real life#1 - tables
A previous year’s project calculated the friction of a stunt man on a
surface. The user had to choose two surfaces (the clothing the
stuntman wore, and the material he/she was sliding to), and whether
it was a dry or wet surface. All was contained in 1 variable:
Material 1
Material 2
leather
leather
leather
concrete
leather
wood
leather
clay
leather
weed
strings..
Friction
dry
wet
0.9
0.1
0.7
0.4
0.8
0.3
0.6
0.2
0.61 0.52
Numerical data..
Strings..
A BIG CELLARRAY!
data={…};
2
Real life #2 - GUI
3
1
10/17/2010
Dialog Boxes: overview
Dialog boxes are “popup windows” that allows another
means to communicate with the user.
There are dialog boxes to collect input:
inputdlg(), listdlg(), menu(), questdlg()
And there are dialog boxes to produce output:
msgbox(), warndlg()
4
Dialog Boxes: timing
Timing wise:
Modal (“waiting”) – they prevent the program from continuing until
the box is closed
Non-modal (“non-waiting”) – the program continues execution
while the box remains open
p
Examples:
Input boxes are generally modal – the program waits for input
before continuing. This is not changeable.
Output boxes default to non-modal (“non-waiting”) but can be
made modal.
5
Dialog Boxes: One Example
Prompt the user for the coefficients to the quadratic
equation, then solve for the roots.
Requirements: Use a GUI to prompt the values, specifically
inputdlg()
…
6
2
10/17/2010
inputdlg(), overview
Collects information from the user
inputdlg() – much like the input() function,
except…
It is presented in a GUI form (Graphical User Interface)
There can be multiple prompts and multiple values provided by
the user.
ALL user-provided information is returned as strings in
a cell array!
☺ That’s why learning about cell-arrays is important!
7
inputdlg(), syntax
Arguments
A cell array of strings: This is the set of prompts for the user. For
example:
prompts={'Coeff "a":', 'Coeff "b":', 'Coeff "c":'}
Return Values
one (1) cell-array of strings: What the user provided in the boxes
Full Example
Cell-array, indicated by the curly braces.
prompts={'Coeff "a":', 'Coeff "b":', 'Coeff "c":'};
coeffs = inputdlg(prompts);
8
inputdlg(), output
When collecting numbers, use str2double() to convert
the cell-array (cargo-ship) into a numerical vector:
coeffs = str2double(coeffs);
9
3
10/17/2010
Application: Quadratic Formula
clear
clc
prompts={'Coeff "a":', 'Coeff "b":', 'Coeff "c":'};
a = 0;
b = 2;
c = 3; %wrong values to make loop run once
% No invalid inputs, or imaginary roots
while (a==0 || (b*b - 4*a*c) < 0)
% Collect the coeff’s from the user
coeffs = inputdlg(prompts);
coeffs = str2double(coeffs);
a = coeffs(1);
b = coeffs(2);
c = coeffs(3);
end
%calculate roots
roots(1) = (-b + sqrt(b*b-4*a*c))/(2*a)
roots(2) = (-b - sqrt(b*b-4*a*c))/(2*a)
10
Application: Quadratic Formula
clear
clc
prompts={'Coeff "a":', 'Coeff "b":', 'Coeff "c":'};
a = 0;
b = 2;
c = 3; %wrong values to make loop run once
% No invalid inputs, or imaginary roots
while (a==0 || (b*b - 4*a*c) < 0)
% Collect the coeff’s from the user
coeffs = inputdlg(prompts);
coeffs = str2double(coeffs);
a = coeffs(1);
Extract the values from the vector:
b = coeffs(2);
Must this be done, or should it be
c = coeffs(3);
end
done for convenience?
%calculate roots
roots(1) = (-b + sqrt(b*b-4*a*c))/(2*a)
roots(2) = (-b - sqrt(b*b-4*a*c))/(2*a)
11
Application: Quadratic Formula
clear
clc
prompts={'Coeff "a":', 'Coeff "b":', 'Coeff "c":'};
a = 0;
b = 2;
c = 3; %wrong values to make loop run once
% No invalid inputs, or imaginary roots
while (coeffs(1)==0 || (coeffs(2)^2-4*coeffs(1)*coeffs(3)<0)
% Collect the coeff’s from the user
coeffs = inputdlg(prompts);
end
Really..
%calculate roots
roots(1) = (-coeffs(2) + sqrt(coeffs(2)^2- ...
4*coeffs(1)*coeffs(3))/(2*coeffs(1))
roots(2) = (-coeffs(2) - sqrt(coeffs(2)^2- ...
4*coeffs(1)*coeffs(3))/(2*coeffs(1))
12
4
10/17/2010
Application: Quadratic Formula
clear
clc
prompts={'Coeff "a":', 'Coeff "b":', 'Coeff "c":'};
a = 0;
b = 2;
c = 3; %wrong values to make loop run once
% No invalid inputs, or imaginary roots
while (a==0 || (b*b - 4*a*c) < 0)
% Collect the coeff’s from the user
coeffs = inputdlg(prompts);
coeffs = str2double(coeffs);
a = coeffs(1);
Since the prompts do not change
b = coeffs(2);
each time it loops, avoid having this
c = coeffs(3);
end
line within the loop. Time & effort
consuming for Matlab.
%calculate roots
roots(1) = (-b + sqrt(b*b-4*a*c))/(2*a)
roots(2) = (-b - sqrt(b*b-4*a*c))/(2*a)
13
Dialog Boxes, help
MATLAB figures (various types of windows: dialog boxes,
plots, etc) have many, many options available to them:
position, size, resizable, etc.
If you want to do more than fundamental dialog boxes,
study the MATLAB help documentation.
F1 = Help
14
Possibility of having up
to 5 arguments!!!
F1 = Help
H l
15
5
10/17/2010
Wrapping Up
GUI stands for Graphical User Interface
Dialog boxes are part of GUI
Most use cell-arrays as arguments and as return values
Non-Modal boxes ((output)
p ) disappear
pp
from the screen.
(Like a pop-up window that closes ASAP..)
Modal boxes stay on the screen (input)
inputdlg() was tremendously explained in these
slides
Use F1/help to learn all the others. BY NOW, ALL THE
VOCABULARY USED IN THE HELP HAS BEEN
TAUGHT. YOU CAN FLY BY YOURSELF! ☺
menu(), listdlg()…
6
© Copyright 2026 Paperzz