Method: NMatrix::IO::Matlab::Mat5Reader::MatrixData#read_packed

Defined in:
lib/nmatrix/io/mat5_reader.rb

#read_packed(packedio, options) ⇒ Object



297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
# File 'lib/nmatrix/io/mat5_reader.rb', line 297

def read_packed(packedio, options)
  flags_class, self.nonzero_max = packedio.read([Element, options]).data

  self.matlab_class   = MatReader::MCLASSES[flags_class % 16]

  self.logical        = (flags_class >> 8) % 2 == 1 ? true : false
  self.global         = (flags_class >> 9) % 2 == 1 ? true : false
  self.complex        = (flags_class >> 10) % 2 == 1 ? true : false

  dimensions_tag_data = packedio.read([Element, options])
  self.dimensions     = dimensions_tag_data.data

  begin
    name_tag_data   = packedio.read([Element, options])
    self.matlab_name = name_tag_data.data.is_a?(Array) ? \
     name_tag_data.data.collect { |i| i.chr }.join('') : \
     name_tag_data.data.chr

  rescue ElementDataIOError => e
    STDERR.puts "ERROR: Failure while trying to read Matlab variable name: #{name_tag_data.inspect}"
    STDERR.puts 'Element Tag:'
    STDERR.puts "    #{e.tag}"
    STDERR.puts 'Previously, I read these dimensions:'
    STDERR.puts "    #{dimensions_tag_data.inspect}"
    STDERR.puts "Unpack options were: #{options.inspect}"
    raise(e)
  end

  if self.matlab_class == :mxCELL
    # Read what may be a series of matrices
    self.cells = []
    STDERR.puts("Warning: Cell array does not yet support reading multiple dimensions") if dimensions.size > 2 || (dimensions[0] > 1 && dimensions[1] > 1)
    number_of_cells = dimensions.inject(1) { |prod,i| prod * i }
    number_of_cells.times { self.cells << \
     packedio.read([Element, options]) }

  else
    read_opts = [RawElement, {:bytes => options[:bytes], \
     :endian => :native}]

    if self.matlab_class == :mxSPARSE
      self.column_index = packedio.read(read_opts)
      self.row_index    = packedio.read(read_opts)
    end

    self.real_part   = packedio.read(read_opts)
    self.imaginary_part = packedio.read(read_opts) if self.complex
  end
end