Skip to content

Commit 1a48741

Browse files
committed
simplify, robustness
1 parent 9716ccc commit 1a48741

File tree

13 files changed

+30
-64
lines changed

13 files changed

+30
-64
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ on:
99
jobs:
1010

1111
linux:
12-
runs-on: ubuntu-latest
12+
runs-on: ubuntu-20.04
1313
steps:
1414
- uses: actions/checkout@v2
1515

Readme.md

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
![ci](https://github.com/scivision/matlab-hdf5/workflows/ci/badge.svg)
44

5-
These functions are sorely needed in Matlab itself.
6-
The NetCDF4 functions also work with GNU Octave when octave-netcdf is installed.
5+
These HDF5 and NetCDF4 functions should be built into Matlab itself, but since they're not yet, we provide them.
6+
The NetCDF4 functions also work with GNU Octave when
7+
[octave-netcdf](https://octave.sourceforge.io/netcdf/index.html)
8+
is installed.
79

810
## hdf5
911

@@ -59,12 +61,6 @@ The NetCDF4 functions also work with GNU Octave when octave-netcdf is installed.
5961
6062
## General utilities
6163
62-
* check if a file exists, and is a file (not a folder). Works with very old Matlab and Octave using fallback.
63-
64-
```matlab
65-
is_file(filename)
66-
```
67-
6864
* Check if running on GNU Octave
6965
7066
```matlab

expanduser.m

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,13 @@
4747
end
4848

4949
if isempty(home)
50-
% this is 100x slower than getenv() on Matlab R2020a
51-
home = char(java.lang.System.getProperty("user.home"));
50+
if usejava('jvm')
51+
% this is 100x slower than getenv() on Matlab R2020a
52+
home = char(java.lang.System.getProperty("user.home"));
53+
else
54+
% return unmodified
55+
return
56+
end
5257
end
5358

5459
expanded = fullfile(home, expanded(2:end));

h5exists.m

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,9 @@
77
% exists: boolean
88

99
narginchk(2,2)
10+
validateattributes(filename, {'char'}, {'vector'}, 1)
1011
validateattributes(varname, {'char'}, {'vector'}, 2)
1112

12-
filename = expanduser(filename);
13-
14-
vars = {};
15-
if is_file(filename)
16-
vars = h5variables(filename);
17-
end
18-
19-
exists = any(strcmp(vars, varname(2:end)));
13+
exists = any(strcmp(h5variables(filename), varname(2:end)));
2014

2115
end % function

h5save.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ function h5save(filename, varname, A, sizeA, dtype)
2121
sizeA = size(A);
2222
end
2323

24-
if h5exists(filename, varname)
24+
if isfile(filename) && h5exists(filename, varname)
2525
exist_file(filename, varname, A, sizeA)
2626
else
2727
new_file(filename, varname, A, sizeA)

h5size.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
% fsize: vector of variable size per dimension
88

99
narginchk(2,2)
10+
validateattributes(filename, {'char'}, {'vector'}, 1)
1011
validateattributes(variable, {'char'}, {'vector'}, 2)
1112

1213
finf = h5info(expanduser(filename), variable);

h5variables.m

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@
22
% get dataset names and groups in an HDF5 file
33
narginchk(1,1)
44

5-
filename = expanduser(filename);
5+
names = [];
6+
groups = [];
67

78
% use temporary variable to be R2017b OK
8-
finf = h5info(filename);
9+
finf = h5info(expanduser(filename));
910
ds = finf.Datasets;
11+
if isempty(ds)
12+
return
13+
end
1014

1115
names = {ds(:).Name};
1216

is_file.m

Lines changed: 0 additions & 21 deletions
This file was deleted.

ncexists.m

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,9 @@
77
% exists: boolean
88

99
narginchk(2,2)
10+
validateattributes(filename, {'char'}, {'vector'}, 1)
1011
validateattributes(varname, {'char'}, {'vector'}, 2)
1112

12-
filename = expanduser(filename);
13-
14-
vars = {};
15-
if is_file(filename)
16-
vars = ncvariables(filename);
17-
end
18-
19-
exists = any(strcmp(vars, varname));
13+
exists = any(strcmp(ncvariables(filename), varname));
2014

2115
end % function

ncsave.m

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,8 @@ function ncsave(filename, varname, A, ncdims, dtype)
3232
A = coerce_ds(A, dtype);
3333
end
3434

35-
vars = {};
36-
if is_file(filename)
37-
vars = ncvariables(filename);
38-
end
3935

40-
if any(strcmp(vars, varname))
36+
if isfile(filename) && ncexists(filename, varname)
4137
exist_file(filename, varname, A, sizeA)
4238
else
4339
new_file(filename, varname, A, sizeA, ncdims)

0 commit comments

Comments
 (0)