Class: Matlab::StructMatrix
- Defined in:
- lib/matlab/matrix.rb,
lib/matlab/driver/native/conversions.rb
Overview
The StructMatrix class is used for storing hash values that are sent to or received from MATLAB struct matrices. Usage:
require 'matlab'
struct_matrix = Matlab::StructMatrix.new(17, 1, "name", "age", "married"
17.times do |m|
struct_matrix[m, 0]["name"] = "Bob #{m}"
struct_matrix[m, 0]["age"] = (rand * 100).to_i
struct_matrix[m, 0]["married"] = (rand > 0.5)
end
p struct_matrix[16, 0]
Instance Attribute Summary collapse
-
#names ⇒ Object
readonly
Returns the value of attribute names.
Attributes inherited from Matrix
Class Method Summary collapse
-
.from_matlab(matrix) ⇒ Object
Creates a Matlab::StructMatrix or Ruby Hash from a MATLAB struct matrix.
Instance Method Summary collapse
-
#initialize(m, n, *names) ⇒ StructMatrix
constructor
Creates a new StructMatrix with the given dimensions for row and column size and the names of the attributes.
-
#to_matlab ⇒ Object
Converts the matrix into a MATLAB struct matrix.
Methods inherited from Matrix
Constructor Details
#initialize(m, n, *names) ⇒ StructMatrix
Creates a new StructMatrix with the given dimensions for row and column size and the names of the attributes
72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/matlab/matrix.rb', line 72 def initialize(m, n, *names) super(m, n) @names = names # Populate the matrix with a hash using the names provided m.times do |row_index| n.times do |column_index| @cells[row_index][column_index] = names.inject({}) { |s,e| s.merge( { e.to_s => nil } ) } end end end |
Instance Attribute Details
#names ⇒ Object (readonly)
Returns the value of attribute names.
68 69 70 |
# File 'lib/matlab/matrix.rb', line 68 def names @names end |
Class Method Details
.from_matlab(matrix) ⇒ Object
Creates a Matlab::StructMatrix or Ruby Hash from a MATLAB struct matrix
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/matlab/driver/native/conversions.rb', line 197 def self.from_matlab(matrix) m = Matlab::Driver::Native::API.mxGetM(matrix) n = Matlab::Driver::Native::API.mxGetN(matrix) names = (0...Matlab::Driver::Native::API.mxGetNumberOfFields(matrix)).collect { |i| Matlab::Driver::Native::API.mxGetFieldNameByNumber(matrix, i) } struct_matrix = self.new(m, n, *names) index = 0 m.times do |row_index| n.times do |column_index| names.each do |name| value = Matlab::Driver::Native::API.mxGetField(matrix, index, name) struct_matrix[row_index, column_index][name] = (value.nil? || Matlab::Driver::Native::API.mxIsEmpty(value) || value.to_ruby.to_s == nil.to_matlab.to_s ? nil : value.to_ruby) end index += 1 end end if m == 1 && n == 1 struct_matrix.cells.flatten.first else struct_matrix end end |
Instance Method Details
#to_matlab ⇒ Object
Converts the matrix into a MATLAB struct matrix
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/matlab/driver/native/conversions.rb', line 178 def to_matlab matrix = Matlab::Driver::Native::API.mxCreateStructMatrix(m, n, 0, nil) names.each { |name| Matlab::Driver::Native::API.mxAddField(matrix, name) } index = 0 m.times do |row_index| n.times do |column_index| names.each do |name| value = (@cells[row_index][column_index][name].nil? ? Matlab::Driver::Native::API.mxCreateDoubleScalar(nil.to_matlab) : @cells[row_index][column_index][name].to_matlab) Matlab::Driver::Native::API.mxSetField(matrix, index, name, value) end index += 1 end end matrix end |