Permalink
Browse files

Fix empty arrays and matrices

  * Empty matrices are converted to empty ruby arrays.
  * Empty ruby arrays are converted to an empty MATLAB matrix
  • Loading branch information...
1 parent 16bc793 commit d1cf1b0c700c8bcbc8d8570f681d8c8e5b816b61 @daikini committed May 19, 2008
Showing with 21 additions and 4 deletions.
  1. +6 −0 History.txt
  2. +7 −2 lib/matlab/driver/native/conversions.rb
  3. +1 −1 lib/matlab/version.rb
  4. +7 −1 test/driver/native/test_conversions.rb
View
6 History.txt
@@ -1,3 +1,9 @@
+== 2.0.3 / 2008-05-19
+
+* 2 bug fixes
+ * Empty matrices are converted to empty ruby arrays.
+ * Empty ruby arrays are converted to an empty MATLAB matrix
+
== 2.0.2 / 2008-05-19
* 1 bug fix
View
9 lib/matlab/driver/native/conversions.rb
@@ -50,7 +50,10 @@ def to_matlab
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? }
+ if empty?
+ matrix = Matlab::Matrix.new(0, 0)
+ matrix.to_matlab
+ elsif all? { |value| value.kind_of?(Numeric) || value.nil? }
matrix = Matlab::Matrix.new(size, 1)
each_with_index do |value, index|
@@ -231,7 +234,9 @@ def to_ruby
mxArrayToString(self)
when mxIsLogical(self)
mxIsLogicalScalarTrue(self)
- when (mxGetM(self) > 1 || mxGetN(self) > 1) || (mxGetM(self) == 0 && mxGetN(self) == 0)
+ when (mxGetM(self) == 0 && mxGetN(self) == 0)
+ []
+ when (mxGetM(self) > 1 || mxGetN(self) > 1)
Matlab::Matrix.from_matlab(self)
when mxIsDouble(self)
mxGetScalar(self)
View
2 lib/matlab/version.rb
@@ -4,7 +4,7 @@ module Version
MAJOR = 2
MINOR = 0
- TINY = 2
+ TINY = 3
STRING = [ MAJOR, MINOR, TINY ].join( "." )
#:beta-tag:
View
8 test/driver/native/test_conversions.rb
@@ -66,7 +66,13 @@ def test_matlab_matrix_to_matlab_to_ruby
def test_should_convert_a_0x0_matlab_matrix
matrix = Matlab::Matrix.new(0, 0)
- assert_equal matrix, matrix.to_matlab.to_ruby
+ result = matrix.to_matlab.to_ruby
+ assert_equal [], result
+ end
+
+ def test_should_convert_an_empty_ruby_array
+ array = []
+ assert_equal array, array.to_matlab.to_ruby
end
def test_matlab_cell_matrix_to_matlab_to_ruby

0 comments on commit d1cf1b0

Please sign in to comment.