% File: patientData.m  
%
% Purpose
%
% SStructure demo (with while and for loops).
%
% Pre and Post Conditions
%
% None.
%
% Input(s)
%
% Patient data (name, age (in years) and blood pressure (in mm Hg). 
%
% Output(s)
%
% Patient data and continuation dialog boxes.
%
% Selected Variables Used
%
% patData - Structure array that holds the patient data. Fields are:
%   name - Patient name.
%   age - Patient age in years.
%   sys - Systolic blood pressure (mm Hg).
%   dia - Diastolic blood pressure (mm Hg).
%
% pat_dlg_title - Patient data dialog box title.
% pat_prompt{1} = Patient dialog name prompt.
% pat_prompt{2} = Patient dialog age prompt.
% pat_prompt{3} = Patient dialog systolic pressure prompt.
% pat_prompt{4} = Patient dialog diastolic pressure prompt.
% cont_dlg_title = Continue dialog box title.
% cont_prompt{1} = Continue dialog box prompt;
%
% id - ID number (patient array index).
% exit - Logical flag use to conrol loop. Make true to exit program.
%
% Revision History
%
% Created 12/20/06 by Dr. C. S. Tritt
% N+1 error in output loop corrected 1/11/07

% I recommend clearing the workspace at the start of all programs.
clear;

% Preallocate space for stucture array.
patData(10) = struct('name', [], 'age', [], 'sys', [], 'dia', []);

% Define prompts for patient data entry and continue dailog.
pat_dlg_title = 'Enter Patient Data';
pat_prompt{1} = 'Name: ';
pat_prompt{2} = 'Age (years): ';
pat_prompt{3} = 'Systolic BP (mm Hg): ';
pat_prompt{4} = 'Diastolic BP (mm Hg): ';
cont_dlg_title = 'Continue?';
cont_prompt{1} = 'Do you wish to conintue? (y or n): ';

id = 0; % ID number (patient array index).
exit = false; % Make true to exit program.

while ~exit
    id = id + 1; % Increment ID numeber (index).
    
    % Get data for a patient.
    patResponse = inputdlg(pat_prompt, pat_dlg_title);
    patData(id).name = patResponse{1};
    patData(id).age = str2double(patResponse{2});
    patData(id).sys = str2double(patResponse{3});
    patData(id).dia = str2double(patResponse{4});
    
    % Ask user if they wish to continue.
    contResponse = inputdlg(cont_prompt, cont_dlg_title);
    if contResponse{1} ~= 'y'
        exit = true;
    end
end

for n = 1:id
    fprintf('%s %d %d %d\n', patData(n).name, patData(n).age, ...
        patData(n).sys,  patData(n).dia);
end