2012-08-14 1 views
7

은 내가 MATLAB에서파이썬에서 액세스 할 때 matlab struct를 보존하는 방법은 무엇입니까?

from scipy import io 
mat = io.loadmat('example.mat') 

를 사용하여 액세스 매트 파일을 가지고, example.mat 내가 data2.LON 큼 쉽게 데이터에 액세스 할 수 있습니다, 다음과 같은 구조체

MATLAB에서
>> load example.mat 
    >> data1 

    data1 = 

      LAT: [53x1 double] 
      LON: [53x1 double] 
      TIME: [53x1 double] 
      units: {3x1 cell} 


    >> data2 

    data2 = 

      LAT: [100x1 double] 
      LON: [100x1 double] 
      TIME: [100x1 double] 
      units: {3x1 cell} 

을 포함, 파이썬에서 그렇게 사소하지는 않다. 그래도 몇 가지 옵션을 제공합니다.

mat.clear  mat.get   mat.iteritems mat.keys  mat.setdefault mat.viewitems 
mat.copy  mat.has_key  mat.iterkeys mat.pop   mat.update  mat.viewkeys  
mat.fromkeys mat.items  mat.itervalues mat.popitem  mat.values  mat.viewvalues  

파이썬에서 동일한 구조를 유지할 수 있습니까? 그렇지 않은 경우 데이터에 가장 잘 액세스하는 방법은 무엇입니까? 현재 사용하고있는 파이썬 코드는 사용하기가 매우 어렵습니다.

감사

+0

파이썬에로드 할 때의 모습을 설명 할 수 있습니까? –

+0

또 다른 생각. SciPi를 사용하고 있다면'SciPi.loadmat'을 사용해 보셨습니까? –

+0

예,로드 매트를 시도했습니다. 파이썬의 출력은 사용하기가 어렵습니다. 데이터 또는 데이터 2에서 LON 또는 LAT에 액세스하는 방법을 모르겠습니다. – mikeP

답변

6

matlab에 구조체와 파이썬에 대한이 튜토리얼을 찾을 수

http://docs.scipy.org/doc/scipy/reference/tutorial/io.html

+0

이렇게하면 추가 정보 레이어를 추가 할 수도 있습니다. http://stackoverflow.com/questions/1984714/how-to-access-fields-in-a-struct-imported-from-a-mat -file-loadmat-in-pyth? rq = 1 –

0

(!) *.mat 파일에 저장 중첩 구조의 경우, 확인해야 할 경우 사전에있는 항목이 io.loadmat 출력은 Matlab 구조입니다. I는 구조체들의 어레이에 저장된 MATLAB에서 파이썬 {strut_1, struct_2} I가 추출로 데이터를로드 할 필요가있을 때, 예를 들어

>> thisStruct 

ans = 
     var1: [1x1 struct] 
     var2: 3.5 

>> thisStruct.var1 

ans = 
     subvar1: [1x100 double] 
     subvar2: [32x233 double] 

나서 scipy.io.loadmat nested structures (i.e. dictionaries)

0

에 mergen하여 코드를 사용 매트랩 경우 scipy.io.loadmat으로로드하는 객체의 키 및 값 목록입니다. 그런 다음 이것들을 변수로 어셈블하거나 필요한 경우 사전에 다시 패키지 할 수 있습니다. exec 명령의 사용은 모든 경우에 적절하지 않을 수 있지만 데이터를 처리하려는 중일 때 제대로 작동합니다.

# Load the data into Python  
D= sio.loadmat('data.mat') 

# build a list of keys and values for each entry in the structure 
vals = D['results'][0,0] #<-- set the array you want to access. 
keys = D['results'][0,0].dtype.descr 

# Assemble the keys and values into variables with the same name as that used in MATLAB 
for i in range(len(keys)): 
    key = keys[i][0] 
    val = np.squeeze(vals[key][0][0]) # squeeze is used to covert matlat (1,n) arrays into numpy (1,) arrays. 
    exec(key + '=val')