I used the following code in mathematica to call matlab
In[1]:= Needs["NETLink`"]
matlab = CreateCOMObject["matlab.application"]
In[5]:= matlab@Execute["a=[1 2;3 4]"]
I want to get matlab workspace variable "a" from mathematica and convert it to mathematica matrix. How c开发者_开发百科an i do this with netlink?
I do not know how you connect with MATLAB... your ProgID
doesn't work on mine, and I'm not sure if it is correct either. A simpler and more reliable way to do it would be to create whatever you want in MATLAB and then save it as a .mat
file and import it into Mathematica. Here's a small example:
MATLAB:
a=magic(4)
a =
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
save('file','a');
Mathematica:
a = Transpose@Import["file.mat", {"HDF5", "Datasets", "a"}];
Assuming that you get output of the form
out = "
a =
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
";
you can convert this into Mathematica's format by using ImportString
command:
matrix = ImportString[out, "Table", "IgnoreEmptyLines" -> True,
"HeaderLines" -> 1];
matrix // TableForm
精彩评论