Mathematica Expression to Matlab m-file Converter
Author
Harri Ojanen
Title
Mathematica Expression to Matlab m-file Converter
Description
The functions in this package provide an automated way of generating Matlab m-files from Mathematica expressions.
Category
Educational Materials
Keywords
URL
http://www.notebookarchive.org/2018-10-10qmnm8/
DOI
https://notebookarchive.org/2018-10-10qmnm8
Date Added
2018-10-02
Date Last Modified
2018-10-02
File Size
12.92 kilobytes
Supplements
Rights
Redistribution rights reserved




<<ToMatlab.m
?ToMatlab
ToMatlab[expr] converts the expression expr into matlab syntax and returns it as a String.ToMatlab[expr, name] returns an assignment of expr into name as a String. name can be also a more complicated string, e.g., ToMatlab[If[t,a,b],"function y=iffun(t,a,b)\ny"].The special symbol Colon can be used to denote the matlab colon operator :, and Colon[a,b] for a:b, Colon[a,b,c] for a:b:c.See also WriteMatlab and PrintMatlab.All functions accept an optional last argument that is the maximum line width.
?PrintMatlab
PrintMatlab[expr] or PrintMatlab[expr, name] is like ToMatlab but instead of returning the String, it is printed on the screen. See also ToMatlab and WriteMatlab.
?WriteMatlab
WriteMatlab[expr, file] or WriteMatlab[expr, file, name] Writes the expr in matlab form into the given file. The second form makes this an assignment into the variable name.Example: f = OpenWrite["file.m"]; WriteMatlab[Cos[x]-x, f, y]; Close[f];The file argument can also be a String that gives the name of the file: WriteMatlab[Cos[x]-x, "file.m", y]; achieves the same result as the previous example (but this limits one expression per file).See also ToMatlab and PrintMatlab.
a=(x+y)^20//Expand
20
x
19
x
18
x
2
y
17
x
3
y
16
x
4
y
15
x
5
y
14
x
6
y
13
x
7
y
12
x
8
y
11
x
9
y
10
x
10
y
9
x
11
y
8
x
12
y
7
x
13
y
6
x
14
y
5
x
15
y
4
x
16
y
3
x
17
y
2
x
18
y
19
y
20
y
ToMatlab
ToMatlab
ToMatlab[a]
x.^20+20.*x.^19.*y+190.*x.^18.*y.^2+1140.*x.^17.*y.^3+4845.* ... x.^16.*y.^4+15504.*x.^15.*y.^5+38760.*x.^14.*y.^6+77520.*x.^13.* ... y.^7+125970.*x.^12.*y.^8+167960.*x.^11.*y.^9+184756.*x.^10.*y.^10+ ... 167960.*x.^9.*y.^11+125970.*x.^8.*y.^12+77520.*x.^7.*y.^13+38760.* ... x.^6.*y.^14+15504.*x.^5.*y.^15+4845.*x.^4.*y.^16+1140.*x.^3.* ... y.^17+190.*x.^2.*y.^18+20.*x.*y.^19+y.^20;
ToMatlab[a,"a"]
a=x.^20+20.*x.^19.*y+190.*x.^18.*y.^2+1140.*x.^17.*y.^3+4845.* ... x.^16.*y.^4+15504.*x.^15.*y.^5+38760.*x.^14.*y.^6+77520.*x.^13.* ... y.^7+125970.*x.^12.*y.^8+167960.*x.^11.*y.^9+184756.*x.^10.*y.^10+ ... 167960.*x.^9.*y.^11+125970.*x.^8.*y.^12+77520.*x.^7.*y.^13+38760.* ... x.^6.*y.^14+15504.*x.^5.*y.^15+4845.*x.^4.*y.^16+1140.*x.^3.* ... y.^17+190.*x.^2.*y.^18+20.*x.*y.^19+y.^20;
ToMatlab[a,150]
x.^20+20.*x.^19.*y+190.*x.^18.*y.^2+1140.*x.^17.*y.^3+4845.*x.^16.*y.^4+15504.*x.^15.*y.^5+38760.*x.^14.*y.^6+77520.*x.^13.*y.^7+125970.*x.^12.*y.^8+ ... 167960.*x.^11.*y.^9+184756.*x.^10.*y.^10+167960.*x.^9.*y.^11+125970.*x.^8.*y.^12+77520.*x.^7.*y.^13+38760.*x.^6.*y.^14+15504.*x.^5.*y.^15+4845.*x.^4.* ... y.^16+1140.*x.^3.*y.^17+190.*x.^2.*y.^18+20.*x.*y.^19+y.^20;
ToMatlab[a,"a",150]
a=x.^20+20.*x.^19.*y+190.*x.^18.*y.^2+1140.*x.^17.*y.^3+4845.*x.^16.*y.^4+15504.*x.^15.*y.^5+38760.*x.^14.*y.^6+77520.*x.^13.*y.^7+125970.*x.^12.*y.^8+ ... 167960.*x.^11.*y.^9+184756.*x.^10.*y.^10+167960.*x.^9.*y.^11+125970.*x.^8.*y.^12+77520.*x.^7.*y.^13+38760.*x.^6.*y.^14+15504.*x.^5.*y.^15+4845.*x.^4.* ... y.^16+1140.*x.^3.*y.^17+190.*x.^2.*y.^18+20.*x.*y.^19+y.^20;
PrintMatlab
PrintMatlab
PrintMatlab[a]
x.^20+20.*x.^19.*y+190.*x.^18.*y.^2+1140.*x.^17.*y.^3+4845.* ... x.^16.*y.^4+15504.*x.^15.*y.^5+38760.*x.^14.*y.^6+77520.* ... x.^13.*y.^7+125970.*x.^12.*y.^8+167960.*x.^11.*y.^9+184756.* ... x.^10.*y.^10+167960.*x.^9.*y.^11+125970.*x.^8.*y.^12+77520.* ... x.^7.*y.^13+38760.*x.^6.*y.^14+15504.*x.^5.*y.^15+4845.* ... x.^4.*y.^16+1140.*x.^3.*y.^17+190.*x.^2.*y.^18+20.*x.*y.^19+ ... y.^20;
PrintMatlab[a,"a"]
a=x.^20+20.*x.^19.*y+190.*x.^18.*y.^2+1140.*x.^17.*y.^3+4845.* ... x.^16.*y.^4+15504.*x.^15.*y.^5+38760.*x.^14.*y.^6+77520.* ... x.^13.*y.^7+125970.*x.^12.*y.^8+167960.*x.^11.*y.^9+184756.* ... x.^10.*y.^10+167960.*x.^9.*y.^11+125970.*x.^8.*y.^12+77520.* ... x.^7.*y.^13+38760.*x.^6.*y.^14+15504.*x.^5.*y.^15+4845.* ... x.^4.*y.^16+1140.*x.^3.*y.^17+190.*x.^2.*y.^18+20.*x.*y.^19+ ... y.^20;
PrintMatlab[a,150]
x.^20+20.*x.^19.*y+190.*x.^18.*y.^2+1140.*x.^17.*y.^3+4845.*x.^16.*y.^4+15504.*x.^15.*y.^5+38760.*x.^14.*y.^6+77520.*x.^13.*y.^7+125970.*x.^12.*y.^8+ ... 167960.*x.^11.*y.^9+184756.*x.^10.*y.^10+167960.*x.^9.*y.^11+125970.*x.^8.*y.^12+77520.*x.^7.*y.^13+38760.*x.^6.*y.^14+15504.*x.^5.*y.^15+4845.*x.^4.* ... y.^16+1140.*x.^3.*y.^17+190.*x.^2.*y.^18+20.*x.*y.^19+y.^20;
PrintMatlab[a,"a",150]
a=x.^20+20.*x.^19.*y+190.*x.^18.*y.^2+1140.*x.^17.*y.^3+4845.*x.^16.*y.^4+15504.*x.^15.*y.^5+38760.*x.^14.*y.^6+77520.*x.^13.*y.^7+125970.*x.^12.*y.^8+ ... 167960.*x.^11.*y.^9+184756.*x.^10.*y.^10+167960.*x.^9.*y.^11+125970.*x.^8.*y.^12+77520.*x.^7.*y.^13+38760.*x.^6.*y.^14+15504.*x.^5.*y.^15+4845.*x.^4.* ... y.^16+1140.*x.^3.*y.^17+190.*x.^2.*y.^18+20.*x.*y.^19+y.^20;
WriteMatlab
WriteMatlab
Directory[]
C:\
f=OpenWrite["tomatlab-test1.m"]
OutputStream[tomatlab-test1.m,4]
WriteMatlab[a,f]
WriteMatlab[a,f,"a"]
WriteMatlab[a,f,120]
WriteMatlab[a,f,"a",120]
Close[f]
tomatlab-test1.m
WriteMatlab[a,"tomatlab-test2.m"]
WriteMatlab[a,"tomatlab-test3.m","a"]
WriteMatlab[a,"tomatlab-test4.m",120]
WriteMatlab[a,"tomatlab-test5.m","a",120]


Cite this as: Harri Ojanen, "Mathematica Expression to Matlab m-file Converter" from the Notebook Archive (2002), https://notebookarchive.org/2018-10-10qmnm8

Download

