Class: Matlab::CellMatrix

Inherits:
Matrix
  • Object
show all
Defined in:
lib/matlab/matrix.rb,
lib/matlab/driver/native/conversions.rb

Overview

The CellMatrix class is used for storing values that are sent to or received from MATLAB cell matrices. Usage:

require 'matlab'

cell_matrix = Matlab::CellMatrix.new(20, 400)
20.times { |m| 400.times { |n| cell_matrix[m, n] = rand.to_s } }

Instance Attribute Summary

Attributes inherited from Matrix

#cells, #m, #n

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Matrix

#==, #[], #[]=, #initialize

Constructor Details

This class inherits a constructor from Matlab::Matrix

Class Method Details

.from_matlab(matrix) ⇒ Object

Creates a Matlab::CellMatrix or Ruby Array from a MATLAB cell matrix



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/matlab/driver/native/conversions.rb', line 153

def self.from_matlab(matrix)
  m = Matlab::Driver::Native::API.mxGetM(matrix)
  n = Matlab::Driver::Native::API.mxGetN(matrix)
  
  cell_matrix = self.new(m, n)
  
  index = 0
  n.times do |column_index|
    m.times do |row_index|
      value = Matlab::Driver::Native::API.mxGetCell(matrix, index).to_ruby
      cell_matrix[row_index, column_index] = (value.nil? || value.to_s == nil.to_matlab.to_s ? nil : value)
      index += 1
    end
  end
  
  if m == 1 || n == 1
    cell_matrix.cells.collect { |cell| cell.first }
  else
    cell_matrix
  end
end

Instance Method Details

#to_matlabObject

Converts the matrix into a MATLAB cell matrix



137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/matlab/driver/native/conversions.rb', line 137

def to_matlab
  matrix = Matlab::Driver::Native::API.mxCreateCellMatrix(m, n)
  
  index = 0
  n.times do |column_index|
    m.times do |row_index|
      value = (@cells[row_index][column_index].nil? ? Matlab::Driver::Native::API.mxCreateDoubleScalar(nil.to_matlab) : @cells[row_index][column_index].to_matlab)
      Matlab::Driver::Native::API.mxSetCell(matrix, index, value)
      index += 1
    end
  end

  matrix
end