Skip to content

Commit 3d1f9a6

Browse files
committed
h5exists: 10-100x speedup, scalar variable
1 parent ebf4c19 commit 3d1f9a6

File tree

3 files changed

+27
-22
lines changed

3 files changed

+27
-22
lines changed

+stdlib/+hdf5nc/h5exists.m

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,27 @@
1-
function exists = h5exists(file, vars)
1+
function exists = h5exists(file, variable)
22

33
arguments
44
file (1,1) string {mustBeFile}
5-
vars string
5+
variable string {mustBeScalarOrEmpty}
66
end
77

8-
i = startsWith(vars, "/");
9-
vars(i) = extractAfter(vars(i), 1);
10-
% NOT contains because we want exact string match
11-
exists = ismember(vars, stdlib.hdf5nc.h5variables(file));
8+
exists = false;
9+
10+
if(strlength(variable) == 0)
11+
return
12+
end
13+
14+
if ~startsWith(variable, "/")
15+
variable = "/" + variable;
16+
end
17+
18+
try
19+
h5info(file, variable);
20+
exists = true;
21+
catch e
22+
if e.identifier ~= "MATLAB:imagesci:h5info:unableToFind"
23+
rethrow(e)
24+
end
25+
end
1226

1327
end

+stdlib/h5exists.m

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
function exists = h5exists(file, vars)
1+
function exists = h5exists(file, variable)
22
%% H5EXISTS(file, vars)
33
% check if object(s) exists in HDF5 file
44
%
55
%%% Inputs
66
% * file: data filename
7-
% * varname: path(s) of variable in file
7+
% * variable: path of variable in file
88
%
99
%%% Outputs
10-
% * exists: boolean (scalar or vector)
10+
% * exists: boolean
1111

1212
arguments
1313
file (1,1) string {mustBeFile}
14-
vars string
14+
variable string {mustBeScalarOrEmpty}
1515
end
1616

17-
exists = stdlib.hdf5nc.h5exists(file, vars);
17+
exists = stdlib.hdf5nc.h5exists(file, variable);
1818

1919
end

test/TestHDF5.m

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -97,23 +97,14 @@ function test_exists(tc)
9797
import matlab.unittest.constraints.IsScalar
9898
basic = tc.TestData.basic;
9999

100-
tc.verifyEmpty(stdlib.h5exists(basic, string.empty))
101-
102100
e = stdlib.h5exists(basic, "");
103101

104102
tc.verifyThat(e, IsScalar)
105103
tc.verifyFalse(e)
106104

107-
vars = {'A0', 'A1', 'A2', 'A3', 'A4', '/A0'};
108-
109-
e = stdlib.h5exists(basic, vars);
110-
tc.verifyTrue(isvector(e))
111-
tc.verifyTrue(all(e))
105+
tc.verifyTrue(stdlib.h5exists(basic, "/A0"));
106+
tc.verifyTrue(stdlib.h5exists(basic, "A0"));
112107

113-
% vector
114-
e = stdlib.h5exists(basic, ["/A1", "oops"]);
115-
tc.verifyTrue(isrow(e))
116-
tc.verifyEqual(e, [true, false])
117108
end
118109

119110

0 commit comments

Comments
 (0)