-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreadExcel.m
59 lines (49 loc) · 1.79 KB
/
readExcel.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
% READEXCEL.M
% function xlData = readExcel(xlfile,xlpage,xlInstruct)
% Generic function to read in columns from an Excel spread sheet
% located in file 'xlfile' on page 'xlpage'. 'xlInstruct' is a
% structure with one element for each output in 'xlData'. The fields
% for each element (i.e. 'xlInstruct(1)','(2)',etc) are '.name',
% '.range' (e.g. 'D4:D15'), and '.length'; the last may be empty and
% is intended to force the output vector or array to fill with NaNs if
% the number of readable rows is less than the expected number.
% Output comes in the form 'xlData.namex' = 'valuex', where '.namex'
% is the name string given in 'xlInstruct(x)' and 'valuex' is the
% corresponding vector or 2-D array read from the spread sheet.
%
% Revisions:
% 2015.10.23: Now using "raw" output form of XLSREAD, to improve handling
% of blank or 'NaN' entries (with MATLAB R2015). This simplifies the code greatly.
%
function xlData = readExcel(xlfile,xlpage,xlInstruct)
xlData = struct;
if ~isfield(xlInstruct,'length')
[xlInstruct.length] = deal([]);
end;
if ~isfield(xlInstruct,'type')
[xlInstruct.type] = deal('num');
end;
for i = 1:length(xlInstruct)
xlrng = xlInstruct(i).range;
xltype = xlInstruct(i).type;
if ~isempty(xlrng) % can't handle mixed right now
[~,~,TempRaw] = xlsread(xlfile,xlpage,xlrng);
if strcmp(xltype,'num')
try
warning off;
output = cat(1,TempRaw{:});
warning on;
catch % in case some extra text is added along the spreadsheet column
for j = 1:size(TempRaw,1)
if ~isnumeric(TempRaw{j}), TempRaw{j} = NaN; end;
end;
output = cat(1,TempRaw{:});
end;
else
output = TempRaw;
end;
xlData.(xlInstruct(i).name) = output;
else
xlData.(xlInstruct(i).name) = [];
end; % if ~isempty(xlrng)
end;