function [IV,HeaderRow,Headers] = Spreadsheet2structure(TXT,ULHeaderValue)
% SPREADSHEET2STRUCTURE Reads in a spreadsheet
% and converts it into a structure whos
% field names are taken from the column
% headers in the spreadsheet.
%
% USAGE:
% 1. Read in the file with XLSREAD
% 2. Pass the text component to
% SPREADSHEET2STRUCTURE along
% with the first column header
% 3. SPREADSHEET2STRUCTURE will
% return the strucuture, the row
% number of the header strings
% and the header strings
%
% EXAMPLE
%
% [num,TXT,raw]=xlsread(yourfile);
% [IV,HeaderRow,Headers] = ...
% Spreadsheet2structure(TXT,'customer')
%
% Will take an excel file that looks
% like this:
%
% A B C D
% +--------------------------------
% 1 |
% 2 | customer amount price
% 3 | Smith 10 100
% 4 | Jones 4 9.2
%
% It will skip column A and rows 1 and 2,
% because 'customer' doesn't appear until
% cell B2, and returns:
%
% IV.customer = {'Smith','Jones'};
% IV.amount = {'10','4'}
% IV.price = {'100','9.2'}
% HeaderRow = 2
% Headers = {'customer','amount','price'}
%
% It's not fancy, but it works
% Michael Robbins
% michaelNOrobbinsSPAMusenet@yahoo.com
% robbins@bloomberg.net
i=1;
iLim=256;
HeaderRow = [];
while i<iLim && isempty(HeaderRow)
HeaderRow = strmatch(ULHeaderValue,TXT(:,i));
i=i+1;
end;
Headers = TXT(HeaderRow,:);
LIMIT = 50;
IV = [];
for i=1:length(Headers)
S(1).type = '.';
FNi = regexprep(Headers{i},'\W','_');
% MUST START WITH A LETTER
if isempty(FNi) FNi='empty'; end;
if ~isempty(regexp(FNi(1),'\d')) FNi = ['F' FNi]; end;
% MUST BE UNIQUE
lim = 0;
while lim<LIMIT & ~isempty(IV) & ...
~isempty(strmatch(FNi,fieldnames(IV),'exact'))
lim = lim + 1;
FNi = [FNi '_'];
end;
S(1).subs = FNi;
IV = subsasgn(IV,S,TXT(HeaderRow+1:end,i));
end;