% File: dialogDemo.m  
%
% Purpose
%
% Demonstrates the use of the inputdlg, msgbox, uiwait and menu functions.
%
% Input(s)
%
% User name and age. 
%
% Output(s)
%
% Various dialog boxes.
%
% Variables Used
%
% See code.
%
% Revision History
%
% 12/3/07 by Dr. C. S. Tritt:
%
%   Incorporated uiwait to get expected msgbox modal behavior.
%
% 12/7/06 by Dr. C. S. Tritt: Created. 

% Clear workspace to avoid side effects.

clear;

% Prompt for and get user input.

in_dlg_title = 'User Input';
prompt{1} = 'Name: ';
prompt{2} = 'Age: ';
answer = inputdlg(prompt, in_dlg_title);

% Extract name and age from user input.

name = answer{1};
age = str2double(answer{2});

% Display result.

out_dlg_title = 'Entered Values';
out_string = sprintf(...
    'Entered name: %s.\nEntered age: %3.0f years.', ...
    name, age);
uiwait(msgbox(out_string, out_dlg_title));
 % Modal doesn't work as might be expected, it only blocks other
 % dialogs. Use uiwait to block program until msgbox is closed.

% Prompt for conversion type and convert age.

conv_type = menu('Convert age to...', 'Days', 'Months', 'Decades');
% A cell array of strings might also work as the menu argument.

switch conv_type
    case 1
        conv_age = 365.25*age;
        conv_units = 'days';
    case 2
        conv_age = 12*age;
        conv_units = 'months';
    case 3
        conv_age = age/10;
        conv_units = 'decades';
end

% Display the results.

out_string = sprintf('%s is %.1f %s old.', ... 
    name, conv_age, conv_units);

msgbox(out_string, 'Converted Age');