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


353
354
355
356
# File 'lib/nmatrix/io/mat5_reader.rb', line 353

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



386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
# File 'lib/nmatrix/io/mat5_reader.rb', line 386

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



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

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



381
382
383
384
# File 'lib/nmatrix/io/mat5_reader.rb', line 381

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

#to_aObject



358
359
360
361
362
# File 'lib/nmatrix/io/mat5_reader.rb', line 358

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

#to_rubyObject



364
365
366
367
368
369
370
371
372
# File 'lib/nmatrix/io/mat5_reader.rb', line 364

def to_ruby
  ary = self.to_a

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