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.
ToExpression
. See edited answer (fix is at end). $\endgroup$