-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfullpath.m
57 lines (56 loc) · 1.84 KB
/
fullpath.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
function [fullPathStr] = fullpath(pathStr)
% @brief Absolute path
%
% Find the absolute path to the given path. The given path
% may point to a file, a folder, or a non existing thing.
% @param pathStr A path (relalive or absolute).
% @return The absolute path to the given path.
%
% % Copyright: 2015-2023 Pascal COMBES <[email protected]>
% % Author: Pascal COMBES <[email protected]>
% % Date: December 24th, 2023
% % Version: 1.0
% % License: GPLv3
% % Requires: chdir
% Finds file path, file name and file extension:
if (((size(pathStr, 2) >= 1) && strcmp(pathStr(end), '.')) || ...
((size(pathStr, 2) >= 2) && strcmp(pathStr((end-1):end), '..')))
filename = '';
fileext = '';
filepath = pathStr;
else
[filepath, filename, fileext] = fileparts(pathStr);
end
% Change to file path:
oldPwd = pwd;
while (true)
try
if isempty(filepath)
fullPathStr = fullfile(pwd, [filename, fileext]);
break;
end
if strcmp(filepath, filesep)
fullPathStr = [filepath, filename, fileext];
break;
end
chdir(filepath);
fullPathStr = fullfile(pwd, [filename, fileext]);
break;
catch anyErr
if strcmp(anyErr.identifier, 'chdir:NonExistentFolder')
[filepath, name, ext] = fileparts(filepath);
if ~isempty(ext)
filename = fullfile([name, ext], filename);
else
filename = fullfile(name, filename);
end
else
rethrow(anyErr);
end
end
end
% Return to initial directory:
if ~strcmp(oldPwd, pwd)
chdir('-');
end
end