|
5 | 5 | ids_temp = []; |
6 | 6 | session = 1; |
7 | 7 | f = dir(fullfile(path)); |
| 8 | + |
8 | 9 | % take the first folder |
9 | 10 | for k = 3:length(f) |
10 | 11 | if f(k).isdir == 1 |
11 | 12 | fprintf('Parsing Animal IDs from folder: %s\n',f(k).name); |
12 | 13 | new_path = [path '/' f(k).name]; |
13 | | - files = dir(fullfile([path '/' f(k).name], '/*.csv')); |
| 14 | + ext = {'/*.csv','/*.CSV','/*.xlsx','/*.XLSX'}; |
| 15 | + for i = 1:length(ext) |
| 16 | + files = dir(fullfile([path '/' f(k).name], ext{i})); |
| 17 | + if ~isempty(files) |
| 18 | + break |
| 19 | + end |
| 20 | + end |
14 | 21 | for j = 1:length(files) |
15 | | - % open the csv file |
16 | | - fid = fopen(fullfile(new_path,'/',files(j).name)); |
17 | | - % get the first line |
18 | | - num_cols = fgetl(fid); |
19 | | - % close it |
20 | | - fclose(fid); |
21 | | - % get the number of columns |
22 | | - num_cols = length(find(num_cols==','))+1; |
23 | | - % make the file format |
24 | | - fmt = repmat('%s ',[1,num_cols]); |
25 | | - % re-open the file and parse all its data |
26 | | - fid = fopen(fullfile(new_path,'/',files(j).name)); |
27 | | - data = textscan(fid,fmt,'CollectOutput',1,'Delimiter',','); |
28 | | - data = data{1}; |
29 | | - fclose(fid); |
30 | | - % Search the Animal ID field and parse the ID number |
31 | | - i = 1; |
32 | | - while i <= size(data,1) |
33 | | - if isequal(data{i,1},id_field) |
34 | | - id = sscanf(data{i,2}, '%d'); |
35 | | - break; |
36 | | - end |
37 | | - i = i+1; |
38 | | - end |
| 22 | + [~, ~, ext] = fileparts(fullfile(new_path,'/',files(j).name)); |
| 23 | + if isequal(ext,'.CSV') || isequal(ext,'.csv') |
| 24 | + % open the file |
| 25 | + fid = fopen(fullfile(new_path,'/',files(j).name)); |
| 26 | + % get the first line |
| 27 | + num_cols = fgetl(fid); |
| 28 | + % close it |
| 29 | + fclose(fid); |
| 30 | + % get the number of columns |
| 31 | + num_cols = length(find(num_cols==','))+1; |
| 32 | + % make the file format |
| 33 | + fmt = repmat('%s ',[1,num_cols]); |
| 34 | + % re-open the file and parse all its data |
| 35 | + fid = fopen(fullfile(new_path,'/',files(j).name)); |
| 36 | + data = textscan(fid,fmt,'CollectOutput',1,'Delimiter',','); |
| 37 | + data = data{1}; |
| 38 | + fclose(fid); |
| 39 | + % Search the Animal ID field and parse the ID number |
| 40 | + i = 1; |
| 41 | + while i <= size(data,1) |
| 42 | + if isequal(data{i,1},id_field) |
| 43 | + id = sscanf(data{i,2}, '%d'); |
| 44 | + break; |
| 45 | + end |
| 46 | + i = i+1; |
| 47 | + end |
| 48 | + elseif isequal(ext,'.XLSX') || isequal(ext,'.xlsx') |
| 49 | + [~, ~, data] = xlsread(fullfile(new_path,'/',files(j).name)); |
| 50 | + while i <= size(data,1) |
| 51 | + if isequal(data{i,1},id_field) |
| 52 | + id = data{i,2}; |
| 53 | + break; |
| 54 | + end |
| 55 | + i = i+1; |
| 56 | + end |
| 57 | + end |
| 58 | + |
39 | 59 | % store the animal ids |
40 | 60 | try |
41 | 61 | ids_temp = [ids_temp, id]; |
|
0 commit comments