开发者

mathematica and matlab interaction with NetLink and data exchange between them

开发者 https://www.devze.com 2023-03-08 20:19 出处:网络
I usedthe following code in mathematica to call matlab In[1]:= Needs[\"NETLink`\"] matlab = CreateCOMObject[\"matlab.application\"]

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"}];

mathematica and matlab interaction with NetLink and data exchange between them


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
0

精彩评论

暂无评论...
验证码 换一张
取 消