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


516
517
518
# File 'lib/nmatrix/io/mat5_reader.rb', line 516

def data
  @data
end

#tagObject

Returns the value of attribute tag

Returns:

  • (Object)

    the current value of tag


516
517
518
# File 'lib/nmatrix/io/mat5_reader.rb', line 516

def tag
  @tag
end

Instance Method Details

#ignore_padding(packedio, bytes) ⇒ Object


564
565
566
567
568
569
570
571
572
# File 'lib/nmatrix/io/mat5_reader.rb', line 564

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)

523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
# File 'lib/nmatrix/io/mat5_reader.rb', line 523

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


574
575
576
# File 'lib/nmatrix/io/mat5_reader.rb', line 574

def to_ruby
  data.to_ruby
end

#write_packed(packedio, options) ⇒ Object


519
520
521
# File 'lib/nmatrix/io/mat5_reader.rb', line 519

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