function ProjectRegionalValues_public(regionaldata,annot,nfn)
%% HELP SECTION OF FUNCTION
%  This program will project regional values (e.g. statistics,mean
%  thickness, p-values) to the surface. The regional values can then be
%  viewed using tksurfer. This is how the cortical renderings were done in
%  Carmicheal et al. (1).
%
%  Usage:
%    ProjectRegionalValues_public(regionaldata,annot,nfn)
%    
%  Inputs:
%  regionaldata -- This is a structure with 2 fields. 
%                  The two fields must be named: 
%                       label (to match against annotation file);
%                           ** same names as 'colortable.struct_names' from
%                              [vertices,label,colortable] = read_annotation();
%                       data (contains the value to project to that region)
%                       data should be NxM where N is the number of labels and M is the number of different values to map.    
%                  NOTE: This can also be a file, that when loaded would
%                  have a structure variable regionaldata with the 2 fields.
%
%  annot        -- This is the annotation file from freesurfer. The label names need to match the names in the label field. 
%  nfn          -- Name of the output file (.nii extension)
%
%  Setup:
%  (1) Download Utilities.zip from
%           http://nmr.mgh.harvard.edu/harvardagingbrain/People/AaronSchultz/Download_Page.html
%  (2) Unzip the file.
%  (3) Change the paths under script setup to match your system.
%  (4) Make sure fsaverageprojection.nii is in the same location as ProjectRegionalValues_public.m
%
%  References: 
%    (1) Carmicheal et al. Coevolution of brain structures in amnestic 
%        mild cognitive impairment. Neuroimage. 2012 Oct 24;66C:449-456.
%
%%% Last updated by Donald G. McLaren, May 23rd, 2013 (mclaren.donald@gmail.com)
%%% Copyright (C) 2011-3,  Donald G. McLaren. PhD
%%%
%%% Supported in part by the NIH funded Harvard Aging Brain Study (P01AG036694) and NIH grant F32AG042228
%%%
%%% This program is free software: you can redistribute it and/or modify
%%% it under the terms of the GNU General Public License as published by
%%% the Free Software Foundation, either version 3 of the License, or
%%% any later version.
%%% 
%%% This program is distributed in the hope that it will be useful,
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%%% GNU General Public License for more details.

%% SCRIPT SETUP
addpath /xxx/freesurfer/matlab/
addpath /xxx/Utilities
addpath /xxx/spm8
basefile=[fileparts('ProjectRegionalValues_public.m') filesep 'fsaverageprojection.nii'];

%% PROGRAM BEGINS HERE
if ischar(regionaldata)
    load(regionaldata)    
end
[vertices,label,colortable] = read_annotation(annot);
output=nan(numel(vertices),size(regionaldata.data,2));
for ii=1:numel(regionaldata.label)
    regionid=colortable.table(find(strcmpi(colortable.struct_names,regionaldata.label{ii})),5);
    if ~isempty(regionid)
        for jj=1:size(regionaldata.data,2)
            output(label==regionid,jj)=regionaldata.data(ii,jj);
        end
    end
end
[img,hdr]=openIMG(basefile);
for jj=1:size(regionaldata.data,2)
    writeIMG(hdr,output(:,jj),[nfn '_' num2str(jj) '.nii'])
end
end