# Transformation Matlab assignments to Mathematica expressions

From a simulation software, I obtain in a matlab file my algebro-differential equations in the following way :

C_p_e = C_state/C_c;

C_p_f = I_state/I_i;

R_p_e = R_r*C_p_f;

I_p_e = (Se_p_e-R_p_e)-C_p_e;

For theses expressions, I would like to do two operations : 1) Transform it into equations 2) Conduct substitutions so as to change the names of the variables with nicer names.

For this objectives, I would like to automatisation the creation of the four equations eq1, eq2, eq3, eq4. For the moment, to create these equations, I only do copy/ paste from matlab but i have models with far more equations.

How can I create equations labeled eq1, ...eqn from this list of expressions (or more assignments in matlab) from Matlab in Mathematica?

• What format is the file? Can you import the equations as strings? – march Aug 25 '15 at 15:23
• I have .m file from Matlab. Here i only make copy/ paste from this file. – Bendesarts Aug 25 '15 at 17:16
• Thanks a lot for your very useful help. It seems that the code is very closed to one that I wanted. However, I have a problem at the end with 'list = StringSplit[comm2, ";\n"] Scan[(eqn[#] = list[[#]]) &, Range@Length@list]' It seems that these lines are not working. Is it possible for me to send you my code ? Otherwise, may you tell me how I can easily show you the status of my code. Thanks a lot for your help – Bendesarts Aug 25 '15 at 19:58
• It's not working because I forgot an essential part, which is to do ToExpression. See edited answer (fix is at end). – march Aug 25 '15 at 20:07
• Thank you your code works perfectly. It remains me only the step of variables substitutions to make the equations nicer. But, my code seems not working. May you have a look to my lastest code ? The 3 first commands are identic and after list = StringSplit[comm2, ";"] list = Replace[ list, {cC[p, e] -> fs[t], cC[p, f] -> v[t], rR[p, e] -> fd[t], iI[p, e] -> fm[t], cC[c] = 1/k, iI[i] = m, rR[r] -> c, sSe[p, e] -> fe[t], iI[state] -> Int[fm[t], t], cC[state] -> Int[v[t], t]}, [Infinity]] Scan[(eqn[#] = ToExpression@list[[#]]) &, Range@Length@list] – Bendesarts Aug 25 '15 at 20:37

I can't guarantee that the Import will work properly without having a sample ".m" file, but somewhere in my dim past, I've used MatLab, and I recall that ".m" files are simple text files. Therefore, we can do the following.

Suppose that the file in which you have stored

C_p_e = C_state/C_c;

C_p_f = I_state/I_i;

R_p_e = R_r*C_p_f;

I_p_e = (Se_p_e-R_p_e)-C_p_e;


is called temp.m. Then, we import the file as a String using

commands = Import["temp.m", "String"];


We then want to change the variable names that have underscores with the corresponding notation in MMA (which does not allow underscores in variable names). Here's one possibility. We first replace all capital letters by lower-case ones so as not to conflict with built-in MMA commands (for instance, both C and I are built-in protected symbols in MMA). Since you are using lower-case and upper-case versions of the same letter, I will do, for instance, cC in place of C:

comm1 = StringReplace[commands, {"\n\n" :> "\n", a_?UpperCaseQ :> ToLowerCase[a] <> a}]
(* cC_p_e:=cC_state/cC_c;
cC_p_f:=iI_state/iI_i;
rR_p_e:=rR_r*cC_p_f;
iI_p_e:=(sSe_p_e-rR_p_e)-cC_p_e; *)


(I have also gotten rid of the extra lines in between expressions for convenience.)

Next, we turn the underscores into brackets (in some sense):

comm2 = StringReplace[comm1
, {"_" ~~ pat1 : LetterCharacter .. ~~ "_" ~~ pat2 : LetterCharacter .. :> "[" <> pat1 <> "," <> pat2 <> "]"
, "_" ~~ pat : LetterCharacter .. :> "[" <> pat <> "]"}]
(* cC[p,e]=cC[state]/cC[c];
cC[p,f]=iI[state]/iI[i];
rR[p,e]=rR[r]*cC[p,f];
iI[p,e]=(sSe[p,e]-rR[p,e])-cC[p,e]; *)


Finally, we evaluate this using

comm2 // ToExpression;


You will find, upon testing ?cC that cC[p,e] has been set to cC[state]/cC[c] and cC[p,f] has been set to iI[state]/iI[i].

Now, if you want to make MMA equations, modify the above with the following.

(1) Add "=" -> "==" to the first list of StringReplace replacements.

(2) After the rest has been executed, do

list = StringSplit[comm2, ";"];
Scan[(eqn[#] = ToExpression @ list[[#]]) &, Range@Length@list];


This will assign the equation cC[p,f]==iI[state]/iI[i] to eqn[2], for instance.

• Thank you for your help. comm1 = StringReplace[commands, {"\n\n" :> "\n", "=" -> "=="}] – Bendesarts May 7 '16 at 7:39
• comm1 = StringReplace[commands, {"\n\n" :> "\n", "=" -> "=="}] Can you explain me what the delayed rule "\n\n" :> "\n" is doing ? – Bendesarts May 7 '16 at 7:40