Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for [] getindex/setindex! syntax. Fix #30 #31

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/MAT_HDF5.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

module MAT_HDF5
using HDF5
import Base: read, write, close
import Base: read, write, close, getindex, setindex!
import HDF5: names, exists, HDF5ReferenceObj, HDF5BitsKind

type MatlabHDF5File <: HDF5.DataFile
Expand Down Expand Up @@ -242,6 +242,9 @@ function read(f::MatlabHDF5File, name::ASCIIString)
val
end

getindex(f::MatlabHDF5File, name::ASCIIString) = read(f, name)
getindex(f::MatlabHDF5File, name::Symbol) = read(f, string(name))

names(f::MatlabHDF5File) = filter!(x->x != "#refs#", names(f.plain))
exists(p::MatlabHDF5File, path::ASCIIString) = exists(p.plain, path)

Expand Down Expand Up @@ -480,6 +483,9 @@ function write(parent::MatlabHDF5File, name::ByteString, thing)
m_write(parent, parent.plain, name, thing)
end

setindex!(parent::MatlabHDF5File, thing, name::ByteString) = write(parent, name, thing)
setindex!(parent::MatlabHDF5File, thing, name::Symbol) = write(parent, string(name), thing)

## Type conversion operations ##

type MatlabString; end
Expand Down
12 changes: 10 additions & 2 deletions src/MAT_v5.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

module MAT_v5
using Zlib, HDF5
import Base: read, write, close
import Base: read, write, close, getindex, setindex!
import HDF5: names, exists

type Matlabv5File <: HDF5.DataFile
Expand Down Expand Up @@ -439,11 +439,19 @@ function read(matfile::Matlabv5File, varname::ASCIIString)
data
end

# []-accessor syntax.
getindex(matfile::Matlabv5File, varname::ASCIIString) = read(matfile, varname)
getindex(matfile::Matlabv5File, varname::Symbol) = read(matfile, string(varname))

# Complain about writing to a MAT file
function write(parent::Matlabv5File, name::ByteString, s)
function write(parent::Matlabv5File, name::ASCIIString, s)
error("Writing to a MATLAB v5 file is not currently supported. Create a new file instead.")
end

# For completeness' sake
setindex!(parent::Matlabv5File, s, name::ASCIIString) = write(parent, name, s)
setindex!(parent::Matlabv5File, s, name::Symbol) = write(parent, string(name), s)

# Close MAT file
close(matfile::Matlabv5File) = close(matfile.ios)
end