forked from cab79/Matlab_files
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxls2struct.m
147 lines (123 loc) · 4.35 KB
/
xls2struct.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
function xlsStruct=xls2struct(file,flag)
%xls2struct Load Excel file contents as a structure
% xlsstruct = xls2struct(file) loads the Excel file 'file'. The first row is
% used to generate the field names for the 1x1 structure xlsstruct. Each
% column (excluding the first row) is then converted to a numeric/cell
% array and assigned to the corresponding field name. All columns are
% expected to have equal number of entries.
%
% xlsstructArray = xls2struct(file,'structArray') will return a 1xNumCol
% structure array xlsstructArray. The kth element of the structure will
% have field names corresponding to the first row and values corresponding
% to the kth row.
%
% GENVARNAME is used to generate a valid MATLAB structure field name from
% the first row data.
%
% Blank entries are returned as 'NaN'.
%
% Platform support is dependent on XLSREAD.
%
% %Example: Given this Excel file content (9 columns, 5 rows):
%
% %one two three four ' ' six se ven
% %1 2 3 6 7
% %11 22 three
% % seven
% % 222 33
%
% xlsStruct=xls2struct('example.xls')
% % Where:
% % xlsStruct.one' = 1 11 NaN NaN
% % xlsStruct.two' = 2 22 NaN 222
% % xlsStruct.three' = [3] 'three' [NaN] [33]
% % xlsStruct.four' = NaN NaN NaN NaN
% % xlsStruct.x' = NaN NaN NaN NaN
% % xlsStruct.six' = 6 NaN NaN NaN
% % xlsStruct.seVen' = [7] [NaN] 'seven' [NaN]
%
% xlsStructArray=xls2struct('example.xls','structArray')
% % Where:
% % xlsStructArray =
% %
% % 1x4 struct array with fields:
% % one
% % two
% % three
% % four
% % x
% % six
% % seVen
% See also: xlsread genvarname
%% handle argin
if nargin < 1
error('MATLAB:xls2struct:FileName',...
'Input excel filename must be specified.');
elseif nargin ==1
flag = '';
elseif nargin ==2
if(strcmpi(flag,'structArray'))
flag=true;
else
error('MATLAB:xls2struct:flag',...
'Invalid second argument');
end
else
error('MATLAB:xls2struct:inputArgCount',...
'Invalid number of input arguments.');
end
if ~ischar(file)
error('MATLAB:xls2struct:InputClass','Filename must be a string.');
end
%% XLSREAD the file
try
%obtain numeric and text data (mutually exclusive contents)
[num,txt, raw]=xlsread(file);
catch ME
error('MATLAB:xls2struct:xlsreaderr',...
'XLSREAD was unable to read this file: %s',ME.message);
end
%% Process the data
% The cell array txt contains all the strings in the excel file
% including the first 'header' row which we assume to be variable names
[rows,numVars]=size(raw); %#ok<ASGLU>
%If the first column is all string, then the num matrix has one column
%less, so keep a dedicated index to the columns in num
numColInd=1;
for varInd=1:numVars
%loop through each column in the excel sheet
%Assume first row element in the current column is the variable name
varName=txt{1,varInd};
%since this string might not be a valid MATLAB variable name (it might
%contain spaces, create one from it:
varName=genvarname(varName);
%if there is a string in this column (other than the first one of
%course) we create a cell array for the data.
stringData=txt(2:end,varInd);
strInds=~cellfun(@isempty,stringData);
if( any(strInds) )
%this column contains strings, use cells
varData={};
try %#ok<TRYNC>
%try to convert any numbers present in this column to cells
varData=num2cell( num(:,varInd) );
end
varData(strInds)=stringData(strInds); %#ok<AGROW>
else
%this column only contains numbers, use arrays
varData=num(:,min(size(num,2),varInd));
numColInd=numColInd+1;
if(flag)
%we need a cell array to 'deal' to fields of structure array
varData=num2cell(varData);
end
end
%Use dynamic field names for MATLAB structures
if(flag)
%create structure array as output
[xlsStruct(1:length(varData)).(varName)]=deal(varData{:});
else
%create field arrays as output
xlsStruct.(varName)=varData;
end
end