% tai.m - allows user to input ac-pc coordinates of fudicials and creates subnum_data.tai, saving it in the subjects folder.
% Kyle Mathewson - Nov 11, 2008

%Still need to make it put the output file in the right place, right now
%just the desktop

subnum=input('Subject Number: ','s');
date=input('MRI acquisition data (XXXXXX; mo da yr): ','s');
Error using input
Cannot call INPUT from EVALC.

Error in tai (line 7)
subnum=input('Subject Number: ','s'); 

-Computes the folder name

if size(subnum,2) == 3
    first_dig =  str2num(char(subnum(1,1)));
    subnum_bin = [sprintf('%03.0f',first_dig*100) '-' sprintf('%03.0f',first_dig*100+99)];
    first_dig = str2num(char(subnum(1,1:2)));
    subnum_bin = [sprintf('%03.0f',first_dig*100) '-' sprintf('%03.0f',first_dig*100+99)];

file_name = ['S' subnum 'ac_' date '.tai'];
% out_path = ['/data/mri/' subnum_bin '/'];
out_path = ['E:\Data\mri\tai\'];
boundaries_str = ['236 191 171'];
boundaries = str2num(boundaries_str);

out_file = {'UofI new scaling';'RAI';boundaries_str;'1.00 1.00 1.00'};

fiducials = {'AC Superior Edge';'PC Inferior Edge';'Most Anterior Point';'Most Posterior Point';'Most Superior Point';...
    'Most Inferior Point';'Most Left Point';'Most Right Point';'Nasion';'Left Preauricular';'Right Preauricular'};

cord_checker = zeros(size(fiducials,1),3);

Prompt for the coordinates, Check the number and boundaries, double check with them, then use it

i_fiducial = 1;
while i_fiducial <= size(fiducials,1)
    out_file{2*i_fiducial+3,1} = char(fiducials(i_fiducial,1));
    prompt = [char(fiducials(i_fiducial,1)) ' (Cor Sag Axi): '];
    curr_coordinates_str = input(prompt, 's');
    curr_coordinates = str2num(curr_coordinates_str);

    %If they didn't type in three coordinates
    if size(curr_coordinates,2) < 3
        fprintf('Not enough coordinates inputed, please try again: \r\n');

    %Check to see if any outside the boundaries
    elseif curr_coordinates(1,1) > boundaries(1,1) | curr_coordinates(1,2) > boundaries(1,2) | curr_coordinates(1,3) > boundaries(1,3)
        fprintf('A coordinate was outside the boundaries, please try again: \r\n');

        %Check to see if correct entry
        fprintf('You entered: ');
        answer = input('. Is this correct? (y/n)','s');
        if strcmp(answer,'n') == 1
            fprintf('Please try again: \r\n');
            %Use it
            out_file{2*i_fiducial+4,1} = num2str(curr_coordinates);
            cord_checker(i_fiducial,:) = curr_coordinates;
            i_fiducial = i_fiducial + 1;

-print out the output file line by line

fid = fopen([out_path file_name], 'wt');
for i_row = 1:size(out_file,1)
    curr_row = out_file{i_row,1};
    fprintf(fid, '%s \n', curr_row);