8

For example:

M={}; 
For[n = 0, n < 100, n = n + 1,
  ... ...   
 M=Append[M, Resultmatrix ]; 
 (*Resultmatrix is the intermediate reslut 4X4 matrix*)
]
Export["data.mat", M]

But when loading data.mat in Matlab, I find that M splits into multiple variables with names like Expression1, Expression2, Expression3 .... I also tried XLS format, but each 4x4 matrix in the list of matrices appears in an individual worksheet. So my question is how can I put M in a single variable, thus I could manipulate it.

novice
  • 2,325
  • 1
  • 24
  • 30
  • You can create the list in one go: m = Array[#1 - 1 &, {100, 4, 4}] – ssch Jan 19 '13 at 12:48
  • You could export Flatten[m] and then use reshape(Expression,[100 4 4]) (not sure about syntax) in the other software. – b.gates.you.know.what Jan 19 '13 at 12:53
  • thanks ssch and b.gatessucks ,M is not a constant matrix, posted is just an example, Flatten[m] does work, but I found reshape changed the structure of M. – novice Jan 19 '13 at 13:18
  • @user5463 The structure is different because one uses row major ordering and the other column major ordering to store the arrays. See this question and the answers there for a way to convert (the answers deal with converting in mma... you can do something similar in matlab) – rm -rf Jan 19 '13 at 17:13
  • also relevant: http://mathematica.stackexchange.com/questions/6837/exporting-a-large-multidimensional-sparse-array – s0rce Jan 20 '13 at 03:25
  • rm -rf♦ thanks,first I Flatten[m] in Mma, then reshape(m,4,4,100) in Matlab. Due to the ordering difference, one has to transpose each 4X4 matrtix to regain the original matrix. – novice Jan 21 '13 at 02:02

2 Answers2

11

See the link for import and export examples for mat files also related question

Below examples work:

Export["file.mat", {"Var1" -> {{1, 2, 3}, {3, 4, 5}}, "Var2" -> {{4, 5, 6}}}, "LabeledData"]

So, use labels and Labeled data options.

s.s.o
  • 4,559
  • 2
  • 27
  • 42
1

Use the hdf5 format instead if you have too many dimensions. Saves a lot of pain and both Matlab and Mathematica support the hdf5 format.

Tom Mozdzen
  • 521
  • 5
  • 16