8
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

JuliaでMatlabのMATファイルを読む

Posted at

MAT.jlというパッケージを利用すると,(おそらく)一部の形式で書かれたデータを読むことができます.

Matlabのデータファイルを一部でも読めると,次のような嬉しさがある場合があります.

  • MatlabのプログラムをJuliaで書き直すが,Matlabで作る人工テストデータも一緒に持ってきたい
  • Matlabのコードでランダム生成したデータをそのままJuliaに持ってきたい(乱数がうまく制御できないため,人工データだけはMatlabで作りたい)
  • Matlabのデータ形式(拡張子mat)で行列が配布されているので,Juliaで読みたい・操作したい
a = full(sprand(10, 5, 0.5))
save("a.mat", "a", "-V6")

% aの中身の例
   0.00000   0.19501   0.43008   0.00000   0.99290
   0.00000   0.00000   0.41530   0.00000   0.00000
   0.03694   0.44592   0.00000   0.00000   0.10643
   0.64455   0.98242   0.52705   0.66157   0.00000
   0.00129   0.99386   0.15698   0.00000   0.00000
   0.95067   0.41919   0.00000   0.34563   0.94855
   0.84108   0.71847   0.61328   0.02859   0.00000
   0.00000   0.00000   0.00000   0.00000   0.00890
   0.00000   0.00000   0.00000   0.00000   0.00000
   0.92218   0.00000   0.00000   0.00000   0.26231
file = matopen(data_sample/a.mat)
# MAT.MAT_v5.Matlabv5File(IOStream(<file data_sample/a.mat>),false,#undef)

a = read(file, "a")
# 10×5 Array{Float64,2}:
# 0.0         0.195015  0.430079  0.0        0.992902
# 0.0         0.0       0.415298  0.0        0.0
# 0.0369418   0.445923  0.0       0.0        0.106427
# 0.644554    0.982418  0.527051  0.661571   0.0
# 0.00129234  0.993864  0.156979  0.0        0.0
# 0.950667    0.419189  0.0       0.345633   0.948547
# 0.84108     0.71847   0.613283  0.0285883  0.0
# 0.0         0.0       0.0       0.0        0.00890422
# 0.0         0.0       0.0       0.0        0.0
# 0.922183    0.0       0.0       0.0        0.262313
8
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?