lib/matlab/driver/native/conversions.rb in matlab-ruby-1.0.1 vs lib/matlab/driver/native/conversions.rb in matlab-ruby-2.0.0

- old
+ new

@@ -25,10 +25,14 @@ class NilClass # Converts nil to MATLAB NaN def to_matlab Matlab::Driver::Native::API.mxGetNaN end + + def to_ruby + nil + end end class Float # Converts the value to nil if it is a MATLAB NaN def to_ruby @@ -42,22 +46,48 @@ Matlab::Driver::Native::API.mxCreateDoubleScalar(self) end end class Array + # Converts the array into a 1 Dimensional MATLAB numeric or cell matrix + def to_matlab + if all? { |value| value.kind_of?(Numeric) || value.nil? } + matrix = Matlab::Matrix.new(size, 1) + + each_with_index do |value, index| + matrix[index, 0] = value + end + matrix.to_matlab + else + to_cell_matrix.to_matlab + end + end + # Flattens and converts the array to a 1 Dimensional Matlab::CellMatrix def to_cell_matrix - values = flatten - cell_matrix = Matlab::CellMatrix.new(values.size, 1) + cell_matrix = Matlab::CellMatrix.new(size, 1) - values.each_with_index do |value, index| + each_with_index do |value, index| cell_matrix[index, 0] = value end cell_matrix end end +class Hash + # Converts the hash into a 1 Dimensional MATLAB struct matrix + def to_matlab + struct_matrix = Matlab::StructMatrix.new(1, 1, *keys) + + each do |(key, value)| + struct_matrix[0,0][key] = value + end + + struct_matrix.to_matlab + end +end + module Matlab class Matrix # Converts the matrix into a MATLAB numeric matrix def to_matlab matrix = Matlab::Driver::Native::API.mxCreateDoubleMatrix(m, n, Matlab::Driver::Native::API::MxREAL) @@ -73,11 +103,11 @@ Matlab::Driver::Native::API.mxSetPr(matrix, double_array) matrix end - # Creates a Matlab::Matrix from a MATLAB numeric matrix + # Creates a Matlab::Matrix or Ruby Array from a MATLAB numeric matrix def self.from_matlab(matrix) m = Matlab::Driver::Native::API.mxGetM(matrix) n = Matlab::Driver::Native::API.mxGetN(matrix) matlab_matrix = self.new(m, n) @@ -89,11 +119,15 @@ matlab_matrix[row_index, column_index] = (Matlab::Driver::Native::API.mxIsNaN(double_array[index]) ? nil : double_array[index]) index += 1 end end - matlab_matrix + if m == 1 || n == 1 + matlab_matrix.cells.flatten + else + matlab_matrix + end end end class CellMatrix # Converts the matrix into a MATLAB cell matrix @@ -110,11 +144,11 @@ end matrix end - # Creates a Matlab::CellMatrix from a MATLAB cell matrix + # Creates a Matlab::CellMatrix or Ruby Array from a MATLAB cell matrix 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) @@ -126,11 +160,15 @@ cell_matrix[row_index, column_index] = (value.nil? || value.to_s == nil.to_matlab.to_s ? nil : value) index += 1 end end - cell_matrix + if m == 1 || n == 1 + cell_matrix.cells.collect { |cell| cell.first } + else + cell_matrix + end end end class StructMatrix # Converts the matrix into a MATLAB struct matrix @@ -150,11 +188,11 @@ end matrix end - # Creates a Matlab::StructMatrix from a MATLAB struct matrix + # Creates a Matlab::StructMatrix or Ruby Hash from a MATLAB struct matrix 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) } @@ -169,10 +207,14 @@ end index += 1 end end - struct_matrix + if m == 1 && n == 1 + struct_matrix.cells.flatten.first + else + struct_matrix + end end end end class SWIG::TYPE_p_mxArray_tag \ No newline at end of file