40
$\begingroup$

Is it possible to export the output expressions from Mathematica computations (e.g., equations) in valid MATLAB syntax?

$\endgroup$
  • 4
    $\begingroup$ Which equations? Can you be more specific on what precisely it is you want to do? one equation? a list of them? Cut and paste? Programmatically export? $\endgroup$ – acl Nov 2 '12 at 22:23
  • $\begingroup$ @acl just an equation with Log s exponentials error functions. For example f[x_]=Log[...]+err[..]*Exp[..]. Something that sort. I know there is a command // InputForm but the output has for example $E$ which MATLAB will not understand. I only want to copy paste the equation to MATLAB. Still unclear? $\endgroup$ – Seyhmus Güngören Nov 2 '12 at 23:29
  • $\begingroup$ This is not exactly an answer, but do to the large chat under previous answer I would like to point out that ToMatlab, provides the page where you can find the package, however many struggles with not being able to download it. I had the same problem until I pressed down alt key and pressed the link at the bottom of the page. Might save a bit of time! $\endgroup$ – ALEXANDER Apr 9 '14 at 10:08
47
$\begingroup$

There is the ToMatlab package that will convert Mathematica expressions to MATLAB equivalents. For example:

<<ToMatlab`
Expand[(x + Log@y)^5] // ToMatlab
(* x.^5+5.*x.^4.*log(y)+10.*x.^3.*log(y).^2+10.*x.^2.*log(y).^3+5.* ...
        x.*log(y).^4+log(y).^5; *)

It even conveniently breaks it using ... and can also convert matrices:

RandomInteger[5, {5, 5}] // ToMatlab
(* [5,0,5,3,4;
    5,5,3,0,2;
    1,4,4,4,4;
    0,3,2,5,5;
    4,5,5,1,1]; *)

However, it won't convert general definitions or things that don't make sense in MATLAB, such as patterns.

To install the package, extract the ToMatlab.m file to

FileNameJoin[{$UserBaseDirectory, "Applications"}]
$\endgroup$
  • $\begingroup$ Great! Thank you very much!! $\endgroup$ – Seyhmus Güngören Nov 3 '12 at 0:09
  • 1
    $\begingroup$ sorry but how can I use this package? $\endgroup$ – Seyhmus Güngören Nov 3 '12 at 0:28
  • $\begingroup$ You extract the ToMatlab.m file to $UserBaseDirectory/Applications/ $\endgroup$ – rm -rf Nov 3 '12 at 0:41
  • 1
    $\begingroup$ @TMH When you download the ToMatlab package from the linked site, you get a zip file. Unzip that file and copy the ToMatlab.m file to the location I mentioned. Replace $UserBaseDirectory with whatever is shown when you evaluate that in Mathematica. $\endgroup$ – rm -rf Mar 30 '13 at 18:25
  • 2
    $\begingroup$ Can you in simple steps numerate what we should do and where? Like: 1. Write this code in mathematica! 2. Write this code in Terminal (mac) or similar. 3. Copy this file to that folder!. What do you mean with extracting something to something? I downloaded ToMatlab.m file, what does it mean to extract it? What software to use when extracting it? Thanks for your patience. $\endgroup$ – Your Majesty Jun 8 '15 at 15:22

Not the answer you're looking for? Browse other questions tagged or ask your own question.