# convert Mathematica expressions to Matlab (ToMatlab and Bessel functions)

I have several expressions like the two followings:

    X11 = I omega rho0 phi HankelH1[m, k0 ra];
X12 = -b1 kp1^2 BesselJ[m, kp1 ra];


I want to covert these expressions to Matlab. To this end, I use the package ToMatlab (by the way is there somewhere available a more recent version of it?)

X11 // ToMatlab
X12 // ToMatlab
(* "sqrt(-1).*omega.*phi.*rho0.*HankelH1(m,k0.*ra);
" *)
(* "(-1).*b1.*kp1.^2.*BesselJ(m,kp1.*ra);
" *)


However, this is not the syntax that Matlab uses of the Bessel and Hankel functions. For instance BesselJ[m,x] (Mathematica) => besselj(m,x) (Matlab).

Any ideas how to overcome the problem and avoid modifying on my own the latter expressions?

• a quick fix could be ... // ToLowerCase. But I don't know Matlab enough to assess how general it is. – Kuba Feb 15 '18 at 13:33
• Thanks a lot! I will try it. I hove more general expressions but for the moment I don't see any problem with your fix. – Dimitris Feb 15 '18 at 13:37
• Why not to export the expression in C or Fortran format, and import it to Matlab? – José Antonio Díaz Navas Feb 15 '18 at 19:42

You could modify the ToMatlab package.

Find the section starting with

(*** Symbols *****************************************************************)


ToMatlabaux[BesselJ] = "besselj"
ToMatlabaux[HankelH1] = "besselh"


In these cases the translation was trivial. Only the function name needed to be changed. When the argument order needs changing too, modify the section starting with

(*** Special cases of functions **********************************************)


For example, for the two-argument form of ArcTan, add

ToMatlabaux[ArcTan[x_, y_]] := "atan2(" <> ToMatlabaux[y] <> ", " <> ToMatlabaux[x] <> ")"


This will set up a new rule for ArcTan, in addition to the existing one that handles the single-argument form.