-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimportindex.m
More file actions
77 lines (66 loc) · 2.73 KB
/
Copy pathimportindex.m
File metadata and controls
77 lines (66 loc) · 2.73 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
function datasetout = importindex(workbookFile,sheetName,startRow,endRow)
%IMPORTINDEX Import data from a spreadsheet
% DATA = IMPORTINDEX(FILE) reads data from the first worksheet in the
% Microsoft Excel spreadsheet file named FILE and returns the data as a
% dataset array.
%
% DATA = IMPORTINDEX(FILE,SHEET) reads from the specified worksheet.
%
% DATA = IMPORTINDEX(FILE,SHEET,STARTROW,ENDROW) reads from the specified
% worksheet for the specified row interval(s). Specify STARTROW and
% ENDROW as a pair of scalars or vectors of matching size for
% dis-contiguous row intervals. To read to the end of the file specify an
% ENDROW of inf.
%
% Date formatted cells are converted to MATLAB serial date number format
% (datenum).
% Non-numeric cells are replaced with: NaN
%
% Example:
% HandlsIndex = importindex('HandlsIndex.xlsx','Sheet1',2,88);
%
% See also XLSREAD.
% Auto-generated by MATLAB on 2014/05/05 13:39:27
%% Input handling
% If no sheet is specified, read first sheet
if nargin == 1 || isempty(sheetName)
sheetName = 1;
end
% If row start and end points are not specified, define defaults
if nargin <= 3
startRow = 2;
endRow = 88;
end
%% Import the data, extracting spreadsheet dates in MATLAB serial date number format (datenum)
[~, ~, raw, dateNums] = xlsread(workbookFile, sheetName, sprintf('A%d:K%d',startRow(1),endRow(1)),'' , @convertSpreadsheetDates);
for block=2:length(startRow)
[~, ~, tmpRawBlock,tmpDateNumBlock] = xlsread(workbookFile, sheetName, sprintf('A%d:K%d',startRow(block),endRow(block)),'' , @convertSpreadsheetDates);
raw = [raw;tmpRawBlock]; %#ok<AGROW>
dateNums = [dateNums;tmpDateNumBlock]; %#ok<AGROW>
end
raw(cellfun(@(x) ~isempty(x) && isnumeric(x) && isnan(x),raw)) = {''};
cellVectors = raw(:,[2,3,5,6,7]);
raw = raw(:,[1,4,8,9,10,11]);
dateNums = dateNums(:,[1,4,8,9,10,11]);
%% Replace date strings by MATLAB serial date numbers (datenum)
R = ~cellfun(@isequalwithequalnans,dateNums,raw) & cellfun('isclass',raw,'char'); % Find spreadsheet dates
raw(R) = dateNums(R);
%% Replace non-numeric cells with NaN
R = cellfun(@(x) ~isnumeric(x) && ~islogical(x),raw); % Find non-numeric cells
raw(R) = {NaN}; % Replace non-numeric cells
%% Create output variable
data = reshape([raw{:}],size(raw));
%% Create dataset array
datasetout = dataset;
%% Allocate imported array to column variable names
datasetout.HNDid = data(:,1);
datasetout.file = cellVectors(:,1);
datasetout.condition = cellVectors(:,2);
datasetout.age = data(:,2);
datasetout.sex = cellVectors(:,3);
datasetout.race = cellVectors(:,4);
datasetout.povertyStatus = cellVectors(:,5);
datasetout.removeStart1 = data(:,3);
datasetout.removeStop1 = data(:,4);
datasetout.removeStart2 = data(:,5);
datasetout.removeStop2 = data(:,6);