# I use Save and Get routinely in Mathematica. How can I get saved variables and functions from Mathematica into Matlab?

6 ビュー (過去 30 日間)
Wayne McKinney 2019 年 6 月 5 日
Commented: Walter Roberson 2019 年 6 月 6 日
I use Save and Get routinely in Mathematica. How can I get saved variables and functions from Mathematica into Matlab?

#### 6 件のコメント

Wayne McKinney 2019 年 6 月 5 日
Done
Walter Roberson 2019 年 6 月 5 日
Mathematica has an export to matlab facility.
Wayne McKinney 2019 年 6 月 5 日
Yeah, from 1999. I have been trying all day to get it to work.

サインイン to comment.

### 採用された回答

Walter Roberson 2019 年 6 月 5 日
file_in = 'diaboloid_aij.txt';
file_out = 'diaboloid_aij.m';
Snew = regexprep(S, {'\[(\d+),', '(\d+)\]', '\[', '\]', 'Cos', 'Sin', 'Tan', 'Sec', '([-+*/])\s*\$', '\)\s*\$'}, {'(1+\$1,', '1+\$1)', '(', ')', 'cos', 'sin', 'tan', 'sec', '\$1 ...', ');'}, 'lineanchors');
fid = fopen(file_out, 'w');
fwrite(fid, Snew);
fclose(fid);
clear diaboloid_aij %flush any saved versions of the file
Script diaboloid_aij is now ready to run after you have defined numeric values or symbolic names for the variables.

#### 4 件のコメント

Walter Roberson 2019 年 6 月 5 日
Example output:
coefmatrix(1+0, 1+2) = ((rp + rs)*sec(a))/(4*rp*rs);
coefmatrix(1+0, 1+4) = ((rp + rs)*(5*rp + rs + (3*rp - rs)*cos(2*a))*sec(a)^3)/ ...
(128*rp^3*rs^2);
coefmatrix(1+0, 1+6) = (sec(a)*(-2*rp^4 + 7*rp^3*rs + 5*rp^2*rs^2 - 3*rp*rs^3 + ...
rs^4 + 2*(rp + rs)^2*(rp^2 + 2*rp*rs - rs^2)*sec(a)^2 + ...
rs*(rp + rs)^3*sec(a)^4))/(512*rp^5*rs^4);
The code is not perfect: in some cases it could miss out adding a semi-colon at the end of an expression. In every expression that happened to be in your file, the expression ended with a ')' and I rely on that to kick on the semi-colon.
Wayne McKinney 2019 年 6 月 6 日
Seems to work beautifully! I had to add ... after = on lines 108 and 152.
Walter Roberson 2019 年 6 月 6 日
Ah. The code I was working with originally would have taken care of that, but unfortunately I had some difficulty in getting it to match in "something that is not ) followed by any amount of whitespace followed by end of line"

サインイン to comment.