@@ -35,11 +35,11 @@ export matopen, matread, matwrite, names, exists, @read, @write
35
35
36
36
# Open a MATLAB file
37
37
const HDF5_HEADER = UInt8[0x89 , 0x48 , 0x44 , 0x46 , 0x0d , 0x0a , 0x1a , 0x0a ]
38
- function matopen (filename:: AbstractString , rd:: Bool , wr:: Bool , cr:: Bool , tr:: Bool , ff:: Bool )
38
+ function matopen (filename:: AbstractString , rd:: Bool , wr:: Bool , cr:: Bool , tr:: Bool , ff:: Bool , compress :: Bool )
39
39
# When creating new files, create as HDF5 by default
40
40
fs = filesize (filename)
41
41
if cr && (tr || fs == 0 )
42
- return MAT_HDF5. matopen (filename, rd, wr, cr, tr, ff)
42
+ return MAT_HDF5. matopen (filename, rd, wr, cr, tr, ff, compress )
43
43
elseif fs == 0
44
44
error (" File \" $filename \" does not exist and create was not specified" )
45
45
end
@@ -76,28 +76,28 @@ function matopen(filename::AbstractString, rd::Bool, wr::Bool, cr::Bool, tr::Boo
76
76
seek (rawfid, offset)
77
77
if read! (rawfid, Vector {UInt8} (undef, 8 )) == HDF5_HEADER
78
78
close (rawfid)
79
- return MAT_HDF5. matopen (filename, rd, wr, cr, tr, ff)
79
+ return MAT_HDF5. matopen (filename, rd, wr, cr, tr, ff, compress )
80
80
end
81
81
end
82
82
83
83
close (rawfid)
84
84
error (" \" $filename \" is not a MAT file" )
85
85
end
86
86
87
- function matopen (fname:: AbstractString , mode:: AbstractString )
88
- mode == " r" ? matopen (fname, true , false , false , false , false ) :
89
- mode == " r+" ? matopen (fname, true , true , false , false , false ) :
90
- mode == " w" ? matopen (fname, false , true , true , true , false ) :
91
- # mode == "w+" ? matopen(fname, true , true , true , true , false) :
92
- # mode == "a" ? matopen(fname, false, true , true , false, true ) :
93
- # mode == "a+" ? matopen(fname, true , true , true , false, true ) :
87
+ function matopen (fname:: AbstractString , mode:: AbstractString ; compress :: Bool = false )
88
+ mode == " r" ? matopen (fname, true , false , false , false , false , false ) :
89
+ mode == " r+" ? matopen (fname, true , true , false , false , false , compress ) :
90
+ mode == " w" ? matopen (fname, false , true , true , true , false , compress ) :
91
+ # mode == "w+" ? matopen(fname, true , true , true , true , false, compress ) :
92
+ # mode == "a" ? matopen(fname, false, true , true , false, true, compress ) :
93
+ # mode == "a+" ? matopen(fname, true , true , true , false, true, compress ) :
94
94
error (" invalid open mode: " , mode)
95
95
end
96
96
97
- matopen (fname:: AbstractString ) = matopen (fname, " r" )
97
+ matopen (fname:: AbstractString ; kwargs ... ) = matopen (fname, " r" ; kwargs ... )
98
98
99
- function matopen (f:: Function , args... )
100
- fid = matopen (args... )
99
+ function matopen (f:: Function , args... ; kwargs ... )
100
+ fid = matopen (args... ; kwargs ... )
101
101
try
102
102
f (fid)
103
103
finally
@@ -106,12 +106,15 @@ function matopen(f::Function, args...)
106
106
end
107
107
108
108
"""
109
- matopen(filename [, mode]) -> handle
110
- matopen(f::Function, filename [, mode]) -> f(handle)
109
+ matopen(filename [, mode]; compress = false ) -> handle
110
+ matopen(f::Function, filename [, mode]; compress = false ) -> f(handle)
111
111
112
112
Mode defaults to "r" for read. It can also be "w" for write, or "r+" for
113
113
read or write without creation or truncation.
114
114
115
+ Compression on reading is detected/handled automatically; the compress
116
+ keyword argument only affects write operations.
117
+
115
118
Use with `read`, `write`, `close`, `names`, and `exists`.
116
119
"""
117
120
matopen
@@ -136,13 +139,13 @@ end
136
139
137
140
# Write a dict to a MATLAB file
138
141
"""
139
- matwrite(filename, d::Dict)
142
+ matwrite(filename, d::Dict; compress::Bool = false )
140
143
141
144
Write a dictionary containing variable names as keys and values as values
142
145
to a Matlab file, opening and closing it automatically.
143
146
"""
144
- function matwrite (filename:: AbstractString , dict:: AbstractDict{S, T} ) where {S, T}
145
- file = matopen (filename, " w" )
147
+ function matwrite (filename:: AbstractString , dict:: AbstractDict{S, T} ; compress :: Bool = false ) where {S, T}
148
+ file = matopen (filename, " w" ; compress = compress )
146
149
try
147
150
for (k, v) in dict
148
151
local kstring
@@ -158,3 +161,4 @@ function matwrite(filename::AbstractString, dict::AbstractDict{S, T}) where {S,
158
161
end
159
162
end
160
163
end
164
+
0 commit comments