-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextractDateInfo.m
More file actions
43 lines (39 loc) · 1.5 KB
/
extractDateInfo.m
File metadata and controls
43 lines (39 loc) · 1.5 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
function [mo, yr, datenumVal, dateStr] = extractDateInfo(cellData)
% Extract the month and year from the cell
% try
% % Try splitting the cellData string using the original delimiters
% splitData = strsplit(cellData, {' : ', ' - '});
% catch
% % If the original split fails, use alternative delimiters and trim leading/trailing spaces
% splitData = strsplit(cellData, {':', '-'});
% splitData = strtrim(splitData);
% end
% %splitData = strsplit(cellData, {' : ', ' - '});
% mo = splitData{2};
% yr = splitData{3};
%
% % Remove leading/trailing whitespaces from month and year
% mo = strtrim(mo);
% yr= strtrim(yr);
patterns = {'PERIODO\s*:\s*(\w+)\s*-\s*(\d+)', 'PERIODO\s*:\s*(\w+)\s*-\s*(\d+)', 'PERIODO\s*:\s*(\w+)\s+(\d+)'};
matched = false;
for i = 1:numel(patterns)
match = regexp(cellData, patterns{i}, 'tokens');
if ~isempty(match)
mo = strtrim(match{1}{1});
yr = strtrim(match{1}{2});
matched = true;
break;
end
end
if ~matched
error('Invalid date format');
end
monumber=double(month(mo,'mmmm'));
yrnumber=double(year(yr,'yyyy'));
% Create a datenumber using the extracted month and year
dateStr = sprintf('%02d-%d-%04d', 1, monumber, yrnumber);
datenumVal = datenum(yrnumber, monumber, 1);
% Create a date vector using the datenumber
dateVec = datevec(datenumVal);
end