-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFindDataFile.m
90 lines (87 loc) · 2.83 KB
/
FindDataFile.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
%% Copyright (c) 2018-2018, Yichao Yu <[email protected]>
%
% This library is free software; you can redistribute it and/or
% modify it under the terms of the GNU Lesser General Public
% License as published by the Free Software Foundation; either
% version 3.0 of the License, or (at your option) any later version.
% This library is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
% Lesser General Public License for more details.
% You should have received a copy of the GNU Lesser General Public
% License along with this library.
% Acceptable id:
% Full path of the file.
% Path relative to $PathPrefix, optionally with ".mat" suffix removed.
% [date, time]: Both date and time can be string or number.
% "$(date)_$(time)": with optional "data_" prefix and ".mat" suffix.
function [fname, stamp] = FindDataFile(id)
PathPrefix = Consts().PathPrefix;
if ischar(id) || (isstring(id) && numel(id) == 1)
id = char(id);
if ~endsWith(id, '.mat')
id = [id '.mat'];
end
[path, name, ext] = fileparts(id);
if startsWith(name, 'data_')
stamp = name(6:end);
else
stamp = name;
end
if exist(id, 'file')
fname = id;
return;
end
if isempty(name)
error('Empty file name.');
end
if isempty(path)
parts = split(stamp, '_');
if length(parts) < 2
error('Cannot parse file name: %s', name);
end
name = ['data_' stamp];
path = fullfile(PathPrefix, 'Data', char(parts(1)));
else
path = fullfile(PathPrefix, path);
end
fname = fullfile(path, [name, ext]);
else
if id == 0
try
m = MemoryMap;
id = [m.Data(1).DateStamp, m.Data(1).TimeStamp];
catch
fprintf('mem map not available!')
return
end
elseif numel(id) ~= 2
error('2-element array required');
end
date = to_char('%08d', id(1));
time = to_char('%06d', id(2));
stamp = [date, '_', time];
fname = fullfile(PathPrefix, 'Data', date, ['data_', stamp, '.mat']);
if ~exist(fname, 'file')
fname = fullfile(PathPrefix, 'Data', date, ['data_', stamp, filesep, 'data_', stamp, '.mat']);
end
end
if ~exist(fname, 'file')
error('Cannot find file: %s', id);
end
end
function s = to_char(fmt, i)
if iscell(i)
if numel(i) ~= 1
error('Invalid input');
end
i = i{1};
end
if ischar(i)
s = i;
elseif isstring(i)
s = char(i);
else
s = sprintf(fmt, i);
end
end