0
1

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.

Numpy > fileIO > MATLAB saveコマンドによるasciiファイルを読込む > genfromtxt()使用

Last updated at Posted at 2017-11-17
動作環境
GeForce GTX 1070 (8GB)
ASRock Z170M Pro4S [Intel Z170chipset]
Ubuntu 16.04 LTS desktop amd64
TensorFlow v1.2.1
cuDNN v5.1 for Linux
CUDA v8.0
Python 3.5.2
IPython 6.0.0 -- An enhanced Interactive Python.
gcc (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609
GNU bash, version 4.3.48(1)-release (x86_64-pc-linux-gnu)
scipy v0.19.1
geopandas v0.3.0
MATLAB R2017b (Home Edition)

関連 

MATLAB > 変数の内容をファイルに保存する > save('pi.txt', 'X', '-ascii')

概要

MATLABのsaveによるファイルをNumpyで読込む。

データ 

tri_bef_171118.txt
   3.0000000e+00   2.0000000e+00   7.0000000e+00   1.0000000e+00
   2.0000000e+00   3.0000000e+00   6.0000000e+00   1.0000000e+00
   4.0000000e+00   3.0000000e+00   6.0000000e+00   2.0000000e+00
   3.0000000e+00   1.0000000e+01   6.0000000e+00   1.0000000e+00
   1.1000000e+01   7.0000000e+00   2.0000000e+00   5.0000000e+00
   7.0000000e+00   2.0000000e+00   5.0000000e+00   1.0000000e+00
   8.0000000e+00   1.2000000e+01   6.0000000e+00   1.0000000e+01
   4.0000000e+00   1.2000000e+01   6.0000000e+00   8.0000000e+00
   4.0000000e+00   8.0000000e+00   6.0000000e+00   3.0000000e+00
   8.0000000e+00   1.0000000e+01   6.0000000e+00   3.0000000e+00
   5.0000000e+00   2.0000000e+00   6.0000000e+00   1.0000000e+00
   3.0000000e+00   1.1000000e+01   9.0000000e+00   1.0000000e+00
   3.0000000e+00   7.0000000e+00   1.1000000e+01   1.0000000e+00
   7.0000000e+00   5.0000000e+00   1.1000000e+01   1.0000000e+00
   1.1000000e+01   5.0000000e+00   9.0000000e+00   1.0000000e+00
   6.0000000e+00   1.2000000e+01   4.0000000e+00   2.0000000e+00
   3.0000000e+00   4.0000000e+00   7.0000000e+00   2.0000000e+00
   8.0000000e+00   4.0000000e+00   7.0000000e+00   3.0000000e+00
   1.1000000e+01   4.0000000e+00   2.0000000e+00   7.0000000e+00
   3.0000000e+00   7.0000000e+00   9.0000000e+00   1.1000000e+01

code v0.1

デリミタは空白3つ必要だった。

test_read_matlab_output_171118.py
import numpy as np

tri = np.genfromtxt('tri_bef_171118.txt', delimiter='   ')

print(tri)
$ python3 test_read_matlab_output_171118.py 
[[  3.   2.   7.   1.]
 [  2.   3.   6.   1.]
 [  4.   3.   6.   2.]
 [  3.  10.   6.   1.]
 [ 11.   7.   2.   5.]
 [  7.   2.   5.   1.]
 [  8.  12.   6.  10.]
 [  4.  12.   6.   8.]
 [  4.   8.   6.   3.]
 [  8.  10.   6.   3.]
 [  5.   2.   6.   1.]
 [  3.  11.   9.   1.]
 [  3.   7.  11.   1.]
 [  7.   5.  11.   1.]
 [ 11.   5.   9.   1.]
 [  6.  12.   4.   2.]
 [  3.   4.   7.   2.]
 [  8.   4.   7.   3.]
 [ 11.   4.   2.   7.]
 [  3.   7.   9.  11.]]

code v0.2

数値にマイナス符号があると、デリミタ空白3つでエラーになる。
空白2つに変更した。

test_read_matlab_output_171118.py
import numpy as np

tri = np.genfromtxt('tri_bef_171118.txt', delimiter='  ')

print(tri)
0
1
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?