Class: NMatrix::IO::Matlab::Mat5Reader

Inherits:
MatReader show all
Defined in:
lib/nmatrix/io/mat5_reader.rb

Overview

Reader (and eventual writer) for a version 5 .mat file.

Defined Under Namespace

Classes: Compressed, Element, ElementDataIOError, Header, MatrixData, MatrixDataStruct, RawElement, Tag

Constant Summary collapse

MDTYPE_UNPACK_ARGS =
MatReader::MDTYPE_UNPACK_ARGS.merge({
  :miCOMPRESSED => [Compressed, {}],
  :miMATRIX   => [MatrixData, {}]
})
FIRST_TAG_FIELD_POS =
128

Constants inherited from MatReader

NMatrix::IO::Matlab::MatReader::DTYPE_PACK_ARGS, NMatrix::IO::Matlab::MatReader::ITYPE_PACK_ARGS, NMatrix::IO::Matlab::MatReader::MCLASSES, NMatrix::IO::Matlab::MatReader::MDTYPES, NMatrix::IO::Matlab::MatReader::MDTYPE_TO_DTYPE, NMatrix::IO::Matlab::MatReader::MDTYPE_TO_ITYPE, NMatrix::IO::Matlab::MatReader::NO_REPACK

Instance Attribute Summary collapse

Attributes inherited from MatReader

#byte_order

Instance Method Summary collapse

Constructor Details

#initialize(stream, options = {}) ⇒ Mat5Reader

call-seq:

NMatrix::IO::Mat5Reader.new(stream, options = {}) -> NMatrix

368
369
370
371
# File 'lib/nmatrix/io/mat5_reader.rb', line 368

def initialize(stream, options = {})
  super(stream, options)
  @file_header = seek_and_read_file_header
end

Instance Attribute Details

#file_headerObject (readonly)

:nodoc:


36
37
38
# File 'lib/nmatrix/io/mat5_reader.rb', line 36

def file_header
  @file_header
end

#first_data_fieldObject (readonly)

:nodoc:


36
37
38
# File 'lib/nmatrix/io/mat5_reader.rb', line 36

def first_data_field
  @first_data_field
end

#first_tag_fieldObject (readonly)

:nodoc:


36
37
38
# File 'lib/nmatrix/io/mat5_reader.rb', line 36

def first_tag_field
  @first_tag_field
end

Instance Method Details

#each(&block) ⇒ Object


401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
# File 'lib/nmatrix/io/mat5_reader.rb', line 401

def each(&block)
  stream.each(Element, {:endian => byte_order}) do |element|
    if element.data.is_a?(Compressed)
      StringIO.new(element.data.content, 'rb').each(Element, \
         {:endian => byte_order}) do |compressed_element|
        yield compressed_element.data
      end

    else
      yield element.data
    end
  end

  # Go back to the beginning in case we want to do it again.
  stream.seek(FIRST_TAG_FIELD_POS)

  self
end

#guess_byte_orderObject


389
390
391
392
393
394
# File 'lib/nmatrix/io/mat5_reader.rb', line 389

def guess_byte_order
  stream.seek(Header::BYTE_ORDER_POS)
  mi = stream.read(Header::BYTE_ORDER_LENGTH)
  stream.seek(0)
  mi == 'IM' ? :little : :big
end

#seek_and_read_file_headerObject


396
397
398
399
# File 'lib/nmatrix/io/mat5_reader.rb', line 396

def seek_and_read_file_header
  stream.seek(0)
  stream.read(FIRST_TAG_FIELD_POS).unpack(Header, {:endian => byte_order})
end

#to_aObject


373
374
375
376
377
# File 'lib/nmatrix/io/mat5_reader.rb', line 373

def to_a
  returning(Array.new) do |ary|
    self.each { |el| ary << el }
  end
end

#to_rubyObject


379
380
381
382
383
384
385
386
387
# File 'lib/nmatrix/io/mat5_reader.rb', line 379

def to_ruby
  ary = self.to_a

  if ary.size == 1
    ary.first.to_ruby
  else
    ary.collect { |item| item.to_ruby }
  end
end