-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfullpath.m
73 lines (57 loc) · 1.59 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
function pnamefull = fullpath(pname, style)
pnamefull = '';
currdir = pwd;
if ~exist('pname','var')
return;
end
if ~exist('style','var')
style = 'linux';
end
if strcmp(style, 'linux')
sep = '/';
else
sep = filesep;
end
% If path to file wasn't specified at all, that is, if only the filename was
% provided without an absolute or relative path, the add './' prefix to file name.
if isempty(fileparts(pname))
pname = ['./', pname];
end
% Fix performance issue: its expensive to call cd so avoid it if we can. A valid path is either
% relative or absolute. If pname is already absolute then no need to change folder or anything else
% we are done and simply exist function. We check full path by prepending './'. If a valid path is
% absolute then prepending a './' will make it invalid. JD, Mar 21, 2022
if ispathvalid(pname) && ~ispathvalid(['./', pname])
pnamefull = pname;
pnamefull(pnamefull=='/' | pnamefull=='\') = sep;
return;
end
% If path is file, extract pathname
try
[p0,f0,e0] = fileparts(pname);
if ispathvalid(pname, 'file') || ~isempty(strfind(f0,'*')) || ~isempty(strfind(e0,'*')) %#ok<*STREMP>
% We have a valid file path
cd(p0);
f = f0;
e = e0;
elseif ispathvalid(pname, 'dir')
cd(pname);
f = '';
e = '';
else
return
end
catch ME
cd(currdir)
% rethrow(ME)
return
end
p = pwd;
pnamefull = [p,sep,f,e];
if ~ispathvalid(pnamefull)
pnamefull = '';
cd(currdir);
return;
end
pnamefull(pnamefull=='/' | pnamefull=='\') = sep;
cd(currdir);