@@ -117,6 +117,44 @@ Compression on reading is detected/handled automatically; the `compress`
117
117
keyword argument only affects write operations.
118
118
119
119
Use with `read`, `write`, `close`, `keys`, and `haskey`.
120
+
121
+ ## Examples
122
+
123
+ ### Usage with `read`
124
+
125
+ ```julia-repl
126
+ julia> matwrite("test.mat", Dict("foo" => 1, "bar" => 2)) # create file
127
+ julia> file = matopen("test.mat")
128
+ julia> read(file) # get a dictionary of `key => value` pairs
129
+ julia> read(file, "foo") # read a specific variable
130
+ julia> close(file)
131
+ ```
132
+
133
+ ### Usage with `write`
134
+
135
+ ```julia-repl
136
+ julia> matwrite("test.mat", Dict("foo" => 1, "bar" => 2)) # create file
137
+ julia> file = matopen("test.mat", "w+")
138
+ julia> write(file, "baz", 3) # test.mat now contains the variable "baz"
139
+ julia> close(file)
140
+ ```
141
+
142
+ ```julia-repl
143
+ julia> matwrite("test.mat", Dict("foo" => 1, "bar" => 2)) # create file
144
+ julia> file = matopen("test.mat", "w")
145
+ julia> write(file, "bop", 3) # test.mat now ONLY contains the variable "bop"
146
+ julia> close(file)
147
+ ```
148
+
149
+ ### Usage with `keys` and `haskey`
150
+
151
+ ```julia-repl
152
+ julia> matwrite("test.mat", Dict("foo" => 1, "bar" => 2)) # create file
153
+ julia> file = matopen("test.mat")
154
+ julia> keys(file) # ["bar", "foo"]
155
+ julia> haskey(file, "bar") # true
156
+ julia> close(file)
157
+ ```
120
158
"""
121
159
matopen
122
160
@@ -126,6 +164,15 @@ matopen
126
164
127
165
Return a dictionary of all the variables and values in a Matlab file,
128
166
opening and closing it automatically.
167
+
168
+ ### Example
169
+
170
+ ```julia-repl
171
+ julia> matwrite("test.mat", Dict("foo" => 1, "bar" => 2)) # create file
172
+ julia> data = matread("test.mat")
173
+ julia> keys(data) # KeySet containing "foo" and "bar"
174
+ julia> data["foo"] # 1
175
+ ```
129
176
"""
130
177
function matread (filename:: AbstractString )
131
178
file = matopen (filename)
@@ -140,10 +187,55 @@ end
140
187
141
188
# Write a dict to a MATLAB file
142
189
"""
143
- matwrite(filename, d::Dict; compress::Bool = false, version::String)
190
+ matwrite(filename, d::Dict; compress::Bool = false, version::String = "" )
144
191
145
192
Write a dictionary containing variable names as keys and values as values
146
193
to a Matlab file, opening and closing it automatically.
194
+
195
+ ### Arguments
196
+
197
+ - `filename`: The name of the output file. If `filename` already exists, \
198
+ its contents will be completely replaced.
199
+ - `d`: A dictionary of `key => value` pairs to write.
200
+ - `key` should be a `String` that starts with a letter and contain only alphanumeric characters and underscores.
201
+ - `value` should be `<:Number`, `Array{<:Number}`, `String`, or `Bool`. `Tuple`, \
202
+ `Nothing` and custom `struct`s will not work.
203
+
204
+ ### Optional Arguments
205
+
206
+ - `compress`: If `true`, the output data are compressed. Default `false`.
207
+ - `version`: A `String` giving the target version. If equal to `"v4"`, \
208
+ write to Matlab v4, otherwise write to Matlab v7.3. Default `""`.
209
+
210
+ ### Examples
211
+
212
+ Write a dictionary of various types:
213
+
214
+ ```julia-repl
215
+ julia> d = Dict(
216
+ "var1" => 1,
217
+ "var2" => 4.0 + 5.0*im,
218
+ "var3" => [1.0, 2.0, 3.0],
219
+ "var4" => rand(UInt8, 4, 4),
220
+ "var5" => true,
221
+ "var6" => "foo")
222
+ julia> matwrite("test.mat", d)
223
+ ```
224
+
225
+ Add a variable to an existing file:
226
+
227
+ ```julia-repl
228
+ julia> d = Dict{String, Any}("var7" => "bar") # note use of `Any`
229
+ julia> merge!(d, matread("test.mat"))
230
+ julia> matwrite("test.mat", d) # "var7" is added to test.mat
231
+ ```
232
+
233
+ Completely replace an existing file:
234
+
235
+ ```julia-repl
236
+ julia> d = Dict{String, Any}("foo" => "bar")
237
+ julia> matwrite("test.mat", d) # "foo" is now the only variable in test.mat
238
+ ```
147
239
"""
148
240
function matwrite (filename:: AbstractString , dict:: AbstractDict{S, T} ; compress:: Bool = false , version:: String = " " ) where {S, T}
149
241
0 commit comments