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

Inherits:
Struct
  • Object
show all
Includes:
Packable
Defined in:
lib/nmatrix/io/mat5_reader.rb

Overview

:nodoc:

Direct Known Subclasses

RawElement

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#dataObject

Returns the value of attribute data

Returns:

  • (Object)

    the current value of data


499
500
501
# File 'lib/nmatrix/io/mat5_reader.rb', line 499

def data
  @data
end

#tagObject

Returns the value of attribute tag

Returns:

  • (Object)

    the current value of tag


499
500
501
# File 'lib/nmatrix/io/mat5_reader.rb', line 499

def tag
  @tag
end

Instance Method Details

#ignore_padding(packedio, bytes) ⇒ Object


543
544
545
546
547
548
549
550
# File 'lib/nmatrix/io/mat5_reader.rb', line 543

def ignore_padding(packedio, bytes)
  if bytes > 0
    #STDERR.puts "Ignored #{8 - bytes} on #{self.tag.data_type}"
    ignored = packedio.read(8 - bytes)
    ignored_unpacked = ignored.unpack("C*")
    raise(IOError, "Nonzero padding detected: #{ignored_unpacked}") if ignored_unpacked.any? { |i| i != 0 }
  end
end

#read_packed(packedio, options) ⇒ Object

Raises:

  • (ArgumentError)

506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
# File 'lib/nmatrix/io/mat5_reader.rb', line 506

def read_packed(packedio, options)
  raise(ArgumentError, 'Missing mandatory option :endian.') unless options.has_key?(:endian)

  tag = packedio.read([Tag, {:endian => options[:endian]}])
  data_type = MDTYPE_UNPACK_ARGS[tag.data_type]

  self.tag = tag

  raise ElementDataIOError.new(tag, "Unrecognized Matlab type #{tag.raw_data_type}") if data_type.nil?

  if tag.bytes == 0
    self.data = []

  else
    number_of_reads = data_type[1].has_key?(:bytes) ? tag.bytes / data_type[1][:bytes] : 1
    data_type[1].merge!({:endian => options[:endian]})

    if number_of_reads == 1
      self.data = packedio.read(data_type)

    else
      self.data =
        returning(Array.new) do |ary|
        number_of_reads.times { ary << packedio.read(data_type) }
      end
    end

    begin
      ignore_padding(packedio, (tag.bytes + tag.size) % 8) unless [:miMATRIX, :miCOMPRESSED].include?(tag.data_type)

    rescue EOFError
      STDERR.puts self.tag.inspect
      raise(ElementDataIOError.new(tag, "Ignored too much"))
    end
  end
end

#to_rubyObject


552
553
554
# File 'lib/nmatrix/io/mat5_reader.rb', line 552

def to_ruby
  data.to_ruby
end

#write_packed(packedio, options) ⇒ Object


502
503
504
# File 'lib/nmatrix/io/mat5_reader.rb', line 502

def write_packed packedio, options
  packedio << [tag, {}] << [data, {}]
end