-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathimportfile.m
62 lines (52 loc) · 6.53 KB
/
importfile.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
function GXSubjectInfoBehavioralData = importfile(workbookFile, sheetName, dataLines)
%IMPORTFILE Import data from a spreadsheet
% GXSUBJECTINFOBEHAVIORALDATA = IMPORTFILE(FILE) reads data from the
% first worksheet in the Microsoft Excel spreadsheet file named FILE.
% Returns the data as a cell array.
%
% GXSUBJECTINFOBEHAVIORALDATA = IMPORTFILE(FILE, SHEET) reads from the
% specified worksheet.
%
% GXSUBJECTINFOBEHAVIORALDATA = IMPORTFILE(FILE, SHEET, DATALINES)
% reads from the specified worksheet for the specified row interval(s).
% Specify DATALINES as a positive scalar integer or a N-by-2 array of
% positive scalar integers for dis-contiguous row intervals.
%
% Example:
% GXSubjectInfoBehavioralData = importfile("D:\GX\Data\GX_Subject Info & Behavioral Data.xlsx", "All Behavioral", [2, 90]);
%
% See also READTABLE.
%
% Auto-generated by MATLAB on 18-May-2020 20:34:04
%% 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 <= 2
dataLines = [2, 90];
end
%% Setup the Import Options and import the data
opts = spreadsheetImportOptions("NumVariables", 79);
% Specify sheet and range
opts.Sheet = sheetName;
opts.DataRange = "A" + dataLines(1, 1) + ":CA" + dataLines(1, 2);
% Specify column names and types
opts.VariableNames = ["Sub", "SessionlabedinNotes", "Session", "FileNum", "VarName5", "Initial", "Date", "StartTime", "EndTime", "StimAmpmA", "ARMS", "PQ1", "PQ2", "PQ3_0YN10", "PQ3_1", "PQ4YN10", "PQ5YN", "PQ6_0YN", "PQ6_1", "PQ6_2", "PQ7_0YN", "PQ7_1", "PQ7_2", "PQ8_0YN", "PQ8_1", "PQ8_2", "ADPreSeverity1", "ADPreSeverity2", "ADPreSeverity3", "ADPreSeverity4", "ADPreSeverity5", "ADPreSeverity6", "ADPreSeverity7", "ADPreSeverity8", "ADPreSeverity9", "ADPreSeverity10", "ADPreRelation1", "ADPreRelation2", "ADPreRelation3", "ADPreRelation4", "ADPreRelation5", "ADPreRelation6", "ADPreRelation7", "ADPreRelation8", "ADPreRelation9", "ADPreRelation10", "KarolSleepPre", "KarolDiscPre", "KarolPainPre", "KarolMoodPre", "KarolAnxPre", "KarolEnergyPre", "KarolSleepPost", "KarolDiscPost", "KarolPainPost", "KarolMoodPost", "KarolAnxPost", "KarolEnergyPost", "ADPostSeverity1", "ADPostSeverity2", "ADPostSeverity3", "ADPostSeverity4", "ADPostSeverity5", "ADPostSeverity6", "ADPostSeverity7", "ADPostSeverity8", "ADPostSeverity9", "ADPostSeverity10", "ADPostRelation1", "ADPostRelation2", "ADPostRelation3", "ADPostRelation4", "ADPostRelation5", "ADPostRelation6", "ADPostRelation7", "ADPostRelation8", "ADPostRelation9", "ADPostRelation10", "VarName79"];
opts.VariableTypes = ["string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string"];
% Specify variable properties
opts = setvaropts(opts, ["Sub", "SessionlabedinNotes", "Session", "FileNum", "VarName5", "Initial", "Date", "StartTime", "EndTime", "StimAmpmA", "ARMS", "PQ1", "PQ2", "PQ3_0YN10", "PQ3_1", "PQ4YN10", "PQ5YN", "PQ6_0YN", "PQ6_1", "PQ6_2", "PQ7_0YN", "PQ7_1", "PQ7_2", "PQ8_0YN", "PQ8_1", "PQ8_2", "ADPreSeverity1", "ADPreSeverity2", "ADPreSeverity3", "ADPreSeverity4", "ADPreSeverity5", "ADPreSeverity6", "ADPreSeverity7", "ADPreSeverity8", "ADPreSeverity9", "ADPreSeverity10", "ADPreRelation1", "ADPreRelation2", "ADPreRelation3", "ADPreRelation4", "ADPreRelation5", "ADPreRelation6", "ADPreRelation7", "ADPreRelation8", "ADPreRelation9", "ADPreRelation10", "KarolSleepPre", "KarolDiscPre", "KarolPainPre", "KarolMoodPre", "KarolAnxPre", "KarolEnergyPre", "KarolSleepPost", "KarolDiscPost", "KarolPainPost", "KarolMoodPost", "KarolAnxPost", "KarolEnergyPost", "ADPostSeverity1", "ADPostSeverity2", "ADPostSeverity3", "ADPostSeverity4", "ADPostSeverity5", "ADPostSeverity6", "ADPostSeverity7", "ADPostSeverity8", "ADPostSeverity9", "ADPostSeverity10", "ADPostRelation1", "ADPostRelation2", "ADPostRelation3", "ADPostRelation4", "ADPostRelation5", "ADPostRelation6", "ADPostRelation7", "ADPostRelation8", "ADPostRelation9", "ADPostRelation10", "VarName79"], "WhitespaceRule", "preserve");
opts = setvaropts(opts, ["Sub", "SessionlabedinNotes", "Session", "FileNum", "VarName5", "Initial", "Date", "StartTime", "EndTime", "StimAmpmA", "ARMS", "PQ1", "PQ2", "PQ3_0YN10", "PQ3_1", "PQ4YN10", "PQ5YN", "PQ6_0YN", "PQ6_1", "PQ6_2", "PQ7_0YN", "PQ7_1", "PQ7_2", "PQ8_0YN", "PQ8_1", "PQ8_2", "ADPreSeverity1", "ADPreSeverity2", "ADPreSeverity3", "ADPreSeverity4", "ADPreSeverity5", "ADPreSeverity6", "ADPreSeverity7", "ADPreSeverity8", "ADPreSeverity9", "ADPreSeverity10", "ADPreRelation1", "ADPreRelation2", "ADPreRelation3", "ADPreRelation4", "ADPreRelation5", "ADPreRelation6", "ADPreRelation7", "ADPreRelation8", "ADPreRelation9", "ADPreRelation10", "KarolSleepPre", "KarolDiscPre", "KarolPainPre", "KarolMoodPre", "KarolAnxPre", "KarolEnergyPre", "KarolSleepPost", "KarolDiscPost", "KarolPainPost", "KarolMoodPost", "KarolAnxPost", "KarolEnergyPost", "ADPostSeverity1", "ADPostSeverity2", "ADPostSeverity3", "ADPostSeverity4", "ADPostSeverity5", "ADPostSeverity6", "ADPostSeverity7", "ADPostSeverity8", "ADPostSeverity9", "ADPostSeverity10", "ADPostRelation1", "ADPostRelation2", "ADPostRelation3", "ADPostRelation4", "ADPostRelation5", "ADPostRelation6", "ADPostRelation7", "ADPostRelation8", "ADPostRelation9", "ADPostRelation10", "VarName79"], "EmptyFieldRule", "auto");
% Import the data
GXSubjectInfoBehavioralData = readtable(workbookFile, opts, "UseExcel", false);
for idx = 2:size(dataLines, 1)
opts.DataRange = "A" + dataLines(idx, 1) + ":CA" + dataLines(idx, 2);
tb = readtable(workbookFile, opts, "UseExcel", false);
GXSubjectInfoBehavioralData = [GXSubjectInfoBehavioralData; tb]; %#ok<AGROW>
end
%% Convert to output type
GXSubjectInfoBehavioralData = table2cell(GXSubjectInfoBehavioralData);
numIdx = cellfun(@(x) ~isnan(str2double(x)), GXSubjectInfoBehavioralData);
GXSubjectInfoBehavioralData(numIdx) = cellfun(@(x) {str2double(x)}, GXSubjectInfoBehavioralData(numIdx));
end