Tell me more ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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

share|improve this question
4  
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? – acl Nov 2 '12 at 22:23
@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? – Seyhmus Güngören Nov 2 '12 at 23:29

1 Answer

up vote 16 down vote accepted

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.

share|improve this answer
Great! Thank you very much!! – Seyhmus Güngören Nov 3 '12 at 0:09
sorry but how can I use this package? – Seyhmus Güngören Nov 3 '12 at 0:28
You extract the ToMatlab.m file to $UserBaseDirectory/Applications/ – rm -rf Nov 3 '12 at 0:41
ok. many thanks once again. – Seyhmus Güngören Nov 3 '12 at 3:08
@SeyhmusGüngören could you please tell me how did you do that? I didn't get what rm-rf says with extracing ToMatlab.m file to...? – TMH Mar 30 at 18:09
show 9 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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