I've been using ToMatlab[], available at http://library.wolfram.com/infocenter/MathSource/577/, to convert Mathematica expressions to Matlab in order to numerically simulate a model.
I recently ran into a bug: I try to have Matlab evaluate a function produced by ToMatlab[], but it gives me the following error:
??? Error using ==> vertcat
CAT arguments dimensions are not consistent.
which means I have a non-square matrix (i.e., not a matrix). Searched around and found (one) mention of the bug, but no solution is mentioned: http://groups.google.com/forum/#!searchin/comp.soft-sys.math.mathematica/tomatlab/comp.soft-sys.math.mathematica/cxNZC9IYgfQ/2Q6jP4U4hzkJ
Dug into it a little more, and discovered what the problem was: Converting a symbolic matrix using the original ToMatlab[] routine can introduce an error if the foldlines[] routine folds a line with the "..." literal right before an operator that is both binary and unary (such as + and -) and is not separated from its right operand by whitespace (e.g., "+5" instead of "+ 5").
Reason being is that the Dot-Dot-Dot symbol "..." are interpreted as spaces in matrices, according to the Matlab documentation. This causes the extra element to be introduced. See the appended examples for an illustration.
I was able to fix this bug by padding the "+" operator by replacing "+" with " + " in the Plus conversion pattern "ToMatlabaux[e_ /; Head[e] === Plus]". I made this and similar (but otherwise insignificant) style modifications to the original ToMatlab[] routine and uploaded it to pastebin here:
http://pastebin.com/TcjErHVT - ToMatlab Bugfix
I attempted to contact Dr. Ojanen about this, but I received an error from his old mail server saying that his username no longer existed there.
I informed Wolfram about this too, but they said that it is up to the contributor to update their code in the Library Archive.
[Appended examples]
EDU>> [3 + 5]
ans =
8
EDU>> [3 + ...
5]
ans =
8
EDU>> [3 +...
5]
ans =
3 5
EDU>> [3 (+ 5)]
ans =
3 5
EDU>> [3 ...
+5]
ans =
3 5
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With