-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathExcelMatlab.m
More file actions
268 lines (238 loc) · 9.74 KB
/
ExcelMatlab.m
File metadata and controls
268 lines (238 loc) · 9.74 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
classdef ExcelMatlab < handle
properties (Access = private, Constant)
excelCOM = ExcelCOM()
end
properties (Access = private)
workbook
workbookSheets
fullPathToFile
writePermission = false
end
methods
function self = ExcelMatlab(varargin)
assert( ...
nargin == 1 || nargin == 2, ...
'ExcelMatlab:invalidNumberArgs', ...
'Argument error.');
if nargin == 2
assert( ...
strcmpi(varargin{2}, 'w'), ...
'ExcelMatlab:invalidArgument', ...
'If seeking write permission, use ''w'' or ''W''.');
end
fullPathToFile = varargin{1};
assert( ...
ischar(fullPathToFile), ...
'ExcelMatlab:invalidPath', ...
'Path must be a string.');
if nargin == 1
workbook = self.openWorkbookForReading(fullPathToFile);
else
workbook = self.openWorkbookForWriting(fullPathToFile);
self.confirmWritableFile(workbook, fullPathToFile);
end
self.workbook = workbook;
self.workbookSheets = workbook.Sheets;
self.fullPathToFile = fullPathToFile;
end
function delete(self)
if ~isempty(self.workbook)
self.workbook.Close();
end
end
end
methods (Access = private)
function workbook = openWorkbookForWriting(self, fullPathToFile)
try
workbook = self.excelCOM.Workbooks.Open(fullPathToFile);
catch
workbook = self.excelCOM.Workbooks.Add();
end
assert( ...
~strcmpi(workbook.FileFormat, 'xlCurrentPlatformText'), ...
'ExcelMatlab:invalidFileFormat', ...
'The specified file is not a valid excel format.');
end
function workbook = openWorkbookForReading(self, fullPathToFile)
try
workbook = self.excelCOM.Workbooks.Open(fullPathToFile, [], true);
catch
error( ...
'ExcelMatlab:openFileForReading', ...
'unable to read from %s\n', ...
fullPathToFile);
end
assert( ...
~strcmpi(workbook.FileFormat, 'xlCurrentPlatformText'), ...
'ExcelMatlab:invalidFileFormat', ...
'The specified file is not a valid excel format.');
end
function confirmWritableFile(self, workbook, fullPathToFile)
try
workbook.SaveAs(fullPathToFile);
self.writePermission = true;
catch
error( ...
'ExcelMatlab:invalidPath', ...
'unable to write to %s\n', ...
fullPathToFile);
end
end
end
methods
function writeToSheet(self, data, sheetName, topLeftRow, topLeftCol)
assert( ...
self.writePermission, ...
'ExcelMatlab:invalidPermission', ...
'Cannot write to file with current permission.');
assert( ...
ischar(sheetName), ...
'ExcelMatlab:invalidSheetName', ...
'Sheet must be a string');
assert( ...
self.isNonnegativeInteger(topLeftRow) ...
&& self.isNonnegativeInteger(topLeftCol), ...
'ExcelMatlab:invalidRowCol', ...
'Row and column must be nonnegative integers.');
bottomRightCol = size(data, 2) + topLeftCol - 1;
bottomRightRow = size(data, 1) + topLeftRow - 1;
rangeName = ExcelMatlab.getRangeName( ...
topLeftCol, ...
bottomRightCol, ...
topLeftRow, ...
bottomRightRow);
sheetToWrite = self.getSheetToWrite(sheetName);
self.tryWritingToSheet(data, sheetToWrite, rangeName);
self.workbook.Save();
end
function cell = readCell(self, sheetName, row, col)
assert( ...
ischar(sheetName), ...
'ExcelMatlab:invalidSheetName', ...
'Sheet must be a string');
assert( ...
self.isNonnegativeInteger(row) ...
&& self.isNonnegativeInteger(col), ...
'ExcelMatlab:invalidRowCol', ...
'Row and column must be nonnegative integers.');
rangeName = ExcelMatlab.getRangeName(col, col, row, row);
sheetToRead = self.getSheetToRead(sheetName);
cell = self.tryReadingFromSheet(sheetToRead, rangeName);
end
function columnData = readNumericColumnRange(self, sheetName, col, firstRow, lastRow)
assert( ...
ischar(sheetName), ...
'ExcelMatlab:invalidSheetName', ...
'Sheet must be a string');
assert( ...
self.isNonnegativeInteger(col) ...
&& self.isNonnegativeInteger(firstRow) ...
&& self.isNonnegativeInteger(lastRow), ...
'ExcelMatlab:invalidRowCol', ...
'Row and column must be nonnegative integers.');
rangeName = ExcelMatlab.getRangeName(col, col, firstRow, lastRow);
sheetToRead = self.getSheetToRead(sheetName);
columnCell = self.tryReadingFromSheet(sheetToRead, rangeName);
columnData = cell2mat(columnCell);
end
function sheetNames = getSheetNames(self)
numberOfSheets = self.workbookSheets.Count;
sheetNames = cell(1, numberOfSheets);
for i = 1:numberOfSheets
sheetNames{i} = self.workbookSheets.Item(i).Name;
end
end
function lastRow = findLastConsecutiveNonemptyRowBelow(self, sheetName, row, col)
rangeName = ExcelMatlab.getRangeName(col, col, row, row);
sheetToRead = self.getSheetToRead(sheetName);
lastRow = sheetToRead.Range(rangeName).End(-4121).Row;
end
end
methods (Access = private, Static)
function is = isNonnegativeInteger(n)
try
assert(~isempty(n), 'ExcelMatlab:invalid', '');
zeros(1, n);
is = n > 0;
catch
is = false;
end
end
end
methods (Access = private)
function sheetToWrite = getSheetToWrite(self, sheetName)
sheetNumber = self.findSheetNumber(sheetName);
if sheetNumber
sheetToWrite = self.workbookSheets.Item(sheetNumber);
else
sheetToWrite = self.addNewSheet(sheetName);
end
end
function sheetToRead = getSheetToRead(self, sheetName)
sheetNumber = self.findSheetNumber(sheetName);
if sheetNumber
sheetToRead = self.workbookSheets.Item(sheetNumber);
else
error( ...
'ExcelMatlab:invalidSheet', ...
'Sheet specified not found in workbook.');
end
end
function sheetNumber = findSheetNumber(self, sheetName)
numberOfSheets = self.workbookSheets.Count;
namesOfSheets = cell(1, numberOfSheets);
for i = 1:numberOfSheets
namesOfSheets{i} = self.workbookSheets.Item(i).Name;
end
[~, sheetNumber] = ismember(sheetName, namesOfSheets);
end
function newSheet = addNewSheet(self, sheetName)
numberOfSheets = self.workbookSheets.Count;
self.workbookSheets.Add([], self.workbookSheets.Item(numberOfSheets));
numberOfSheets = numberOfSheets + 1;
newSheet = self.workbookSheets.Item(numberOfSheets);
newSheet.Name = sheetName;
end
end
methods (Static)
function rangeName = getRangeName(firstColumn, lastColumn, firstRow, lastRow)
firstColumnName = ExcelMatlab.getColumnNameFromNumber(firstColumn);
lastColumnName = ExcelMatlab.getColumnNameFromNumber(lastColumn);
rangeName = [ ...
firstColumnName, ...
num2str(firstRow), ...
':', ...
lastColumnName, ...
num2str(lastRow)];
end
end
methods (Static, Access = private)
function columnName = getColumnNameFromNumber(n)
numberOfLettersInAlphabet = 26;
if n > numberOfLettersInAlphabet
offset = floor((n - 1) / numberOfLettersInAlphabet);
firstLetter = char('A' + offset - 1);
secondLetter = char('A' + mod(n - 1, numberOfLettersInAlphabet));
columnName = [firstLetter, secondLetter];
else
columnName = char('A' + n - 1);
end
end
end
methods (Access = private)
function tryWritingToSheet(self, data, sheetToWrite, rangeName)
try
sheetToWrite.Range(rangeName).Value = data;
catch
error('ExcelMatlab:invalidRange', 'Invalid range specified');
end
end
function data = tryReadingFromSheet(self, sheetToRead, rangeName)
try
data = sheetToRead.Range(rangeName).Value;
catch
error('ExcelMatlab:invalidRange', 'Invalid range specified');
end
end
end
end