Skip to content

Commit b025438

Browse files
committed
read xlsx files
The program can now read xlsx files apart from csv. Mixed input of xlsx and csv is also allowed. (issue: #28)
1 parent 97024b4 commit b025438

File tree

3 files changed

+63
-9
lines changed

3 files changed

+63
-9
lines changed

segmentation/parse_data.m

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
function [ processed_data ] = parse_data(fn, animal_id, rec_time, centre_x, centre_y)
22
%PARSE_DATA gets the data of interest from file
33

4-
%Read the csv file
5-
[data, header_idx] = read_ethovision(fn,rec_time, centre_x, centre_y);
6-
4+
%Read the csv or xlsx file
5+
[~, ~, ext] = fileparts(fn);
6+
if isequal(ext,'.CSV') || isequal(ext,'.csv')
7+
[data, header_idx] = read_ethovision(fn, rec_time, centre_x, centre_y);
8+
elseif isequal(ext,'.XLSX') || isequal(ext,'.xlsx')
9+
[data, header_idx] = read_ethovision_xlsx(fn, rec_time, centre_x, centre_y);
10+
end
11+
712
%Initialize
813
id = '';
914
skipped_file = '';
@@ -12,13 +17,24 @@
1217
y = {};
1318
damaged_file = 0;
1419
pts = [];
20+
flag = '';
1521

1622
%First find id
1723
i = 1;
1824
while i <= size(data,1)
1925
if isequal(data{i,1},animal_id)
20-
id = sscanf(data{i,2}, '%d');
21-
elseif ~isempty(str2num(data{i,1}))
26+
if isnumeric(data{i,2})
27+
id = data{i,2};
28+
else
29+
id = sscanf(data{i,2}, '%d');
30+
end
31+
end
32+
try
33+
flag = str2num(data{i,1});
34+
catch
35+
flag = data{i,1};
36+
end
37+
if ~isempty(flag)
2238
%if we are here then we should have the id
2339
if isempty(id)
2440
%indicate which file was skipped
@@ -71,9 +87,21 @@
7187
else
7288
for i = 1:length(time)
7389
% extract time, X and Y coordinates
74-
T = sscanf(time{i, 1}, '%f');
75-
X = sscanf(x{i, 1}, '%f');
76-
Y = sscanf(y{i, 1}, '%f');
90+
if isnumeric(time{i, 1})
91+
T = time{i, 1};
92+
else
93+
T = sscanf(time{i, 1}, '%f');
94+
end
95+
if isnumeric(x{i, 1})
96+
X = x{i, 1};
97+
else
98+
X = sscanf(x{i, 1}, '%f');
99+
end
100+
if isnumeric(y{i, 1})
101+
Y = y{i, 1};
102+
else
103+
Y = sscanf(y{i, 1}, '%f');
104+
end
77105
% discard missing samples
78106
if ~isempty(T) && ~isempty(X) && ~isempty(Y)
79107
pts = [pts; T X Y];
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function [ data, count ] = read_ethovision_xlsx(fn, rec_time, centre_x, centre_y);
2+
%READ_ETHOVISION_XLSX same as read_ethovision.m but for .xlsx files
3+
4+
[~, ~, data] = xlsread(fn);
5+
count = 0;
6+
7+
for i = 1:size(data,1)
8+
count = i;
9+
a = find(strcmp(data(i,:),rec_time));
10+
b = find(strcmp(data(i,:),centre_x));
11+
c = find(strcmp(data(i,:),centre_y));
12+
if ~isempty(a) && ~isempty(b) && ~isempty(c)
13+
break
14+
end
15+
end
16+
17+
end
18+

segmentation/set_data.m

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,15 @@
33

44
day = -1;
55
traj = trajectories([]);
6-
files = dir(fullfile(path, '/*.csv') );
6+
7+
ext = {'/*.csv','/*.CSV','/*.xlsx','/*.XLSX'};
8+
for i = 1:length(ext)
9+
files = dir(fullfile(path, ext{i}) );
10+
if ~isempty(files)
11+
break
12+
end
13+
end
14+
715
fprintf('Importing %d trajectories...\n', length(files));
816

917
for i = 1:length(files)

0 commit comments

Comments
 (0)