Skip to content

Commit db1f165

Browse files
committed
exists() does not consider URL a file or folder. add is_url()
Matlab R2024b allows URLs to be treated like files and folders. For our stdlib, we give the option (default false) to not consider URLs as files and folders.
1 parent ded83a8 commit db1f165

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

+stdlib/exists.m

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,16 @@
99
%%% Outputs
1010
% * ok: true if exists
1111

12-
function ok = exists(p)
12+
function y = exists(p)
1313
arguments
1414
p (1,1) string
1515
end
1616

17-
ok = isfile(p) || isfolder(p);
17+
% Matlab >= R2024b allowed URLs to act like files or folders.
18+
% fileattrib() does not consider URLs to be a file or folder
19+
% at least through Matlab R2025a.
20+
21+
y = stdlib.len(p) && fileattrib(p) == 1;
1822

1923
end
2024

+stdlib/is_url.m

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
function y = is_url(s)
2+
arguments
3+
s (1,1) string
4+
end
5+
6+
try
7+
y = startsWith(s, alphanumericsPattern + "://");
8+
catch e
9+
% Matlab < R2020b
10+
if ~strcmp(e.identifier, "MATLAB:UndefinedFunction") && ...
11+
~strcmp(e.identifier, "Octave:undefined-function")
12+
rethrow(e)
13+
end
14+
15+
% https://www.mathworks.com/help/matlab/import_export/work-with-remote-data.html
16+
17+
y = startsWith(s, "http://") || startsWith(s, "https://") || ...
18+
startsWith(s, "ftp://") || startsWith(s, "file://") || ...
19+
startsWith(s, "s3://") || startsWith(s, "hdfs://") || ...
20+
startsWith(s, "wasbs://");
21+
end
22+
23+
end
24+
25+
%!assert (is_url("http://example.com"), true)
26+
%!assert (is_url("//server"), false)

0 commit comments

Comments
 (0)