2012-08-14 7 views
7

Ho un file stuoia che ho accede tramiteCome conservare la struttura matlab quando si accede a Python?

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

da MATLAB, example.mat contiene le seguenti struct

>> 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} 

in MATLAB, posso accedere ai dati facile come data2.LON, ecc. Non è così banale in Python. Mi dà diverse opzioni come

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  

È possibile preservare la stessa struttura in python? In caso negativo, come accedere al meglio ai dati? Il codice Python che sto usando è molto difficile da lavorare.

Grazie

+0

Puoi spiegare come appare quando lo carichi in python? –

+0

Inoltre, un altro pensiero. Se stai usando SciPi hai provato a usare 'SciPi.loadmat'? –

+0

sì, ho provato il loadmat. L'output in python è solo difficile da usare. Non so nemmeno come accedere a LON o LAT in data1 o data2. – mikeP

risposta

6

trovato questo tutorial su MATLAB struct e pitone

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

+0

Questo potrebbe essere in grado di aggiungere un livello di informazioni aggiuntive anche per te: http://stackoverflow.com/questions/1984714/how-to-access-fields-in-a-struct-imported-from-a-mat -file-con-loadmat-in-Pyth? RQ = 1 –

0

(!) Nel caso di strutture nidificate salvati in *.mat file, è necessario verificare se le voci del dizionario che Le uscite io.loadmat sono strutture Matlab. Per esempio, se in Matlab

>> thisStruct 

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

>> thisStruct.var1 

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

quindi utilizzare il codice Mergen in scipy.io.loadmat nested structures (i.e. dictionaries)

0

Quando ho bisogno di caricare i dati in Python da MATLAB che viene memorizzato in un array di struct {strut_1, struct_2} estraggo un elenco di chiavi e valori dall'oggetto che carico con scipy.io.loadmat. Posso quindi assemblarli in variabili proprie o, se necessario, riconfezionarli in un dizionario. L'uso del comando exec potrebbe non essere appropriato in tutti i casi, ma se stai solo provando a elaborare i dati, funziona bene.

# 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')