Skip to content

Commit 2a9373d

Browse files
committed
Merge pull request #39 from Chilichiller/master
bugfix for complex data
2 parents 1d20faa + 9af2365 commit 2a9373d

File tree

2 files changed

+45
-6
lines changed

2 files changed

+45
-6
lines changed

pymatbridge/examples/matlab_magic.ipynb

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"collapsed": false,
1313
"input": [
1414
"import os\n",
15+
"import numpy as np\n",
1516
"import pymatbridge as pymat\n",
1617
"reload(pymat)"
1718
],
@@ -179,13 +180,47 @@
179180
],
180181
"prompt_number": 7
181182
},
183+
{
184+
"cell_type": "code",
185+
"collapsed": false,
186+
"input": [
187+
"%%matlab -o cmpl\n",
188+
"a = linspace(0.01,6*pi,100);\n",
189+
"cmpl = exp(1i * a);"
190+
],
191+
"language": "python",
192+
"metadata": {},
193+
"outputs": [],
194+
"prompt_number": 8
195+
},
196+
{
197+
"cell_type": "code",
198+
"collapsed": false,
199+
"input": [
200+
"cmpl[2] # complex data works also"
201+
],
202+
"language": "python",
203+
"metadata": {},
204+
"outputs": [
205+
{
206+
"metadata": {},
207+
"output_type": "pyout",
208+
"prompt_number": 9,
209+
"text": [
210+
"array([ 0.92468189+0.3807406j])"
211+
]
212+
}
213+
],
214+
"prompt_number": 9
215+
},
182216
{
183217
"cell_type": "code",
184218
"collapsed": false,
185219
"input": [],
186220
"language": "python",
187221
"metadata": {},
188-
"outputs": []
222+
"outputs": [],
223+
"prompt_number": 9
189224
}
190225
],
191226
"metadata": {}

pymatbridge/matlab_magic.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@
2121

2222
import numpy as np
2323
try:
24-
import tables
24+
import h5py
2525
import scipy.io as sio
2626
has_io = True
2727
except ImportError:
2828
has_io = False
29-
no_io_str = "Must have pytables and scipy.io to perform i/o"
29+
no_io_str = "Must have h5py and scipy.io to perform i/o"
3030
no_io_str += "operations with the Matlab session"
3131

3232
from IPython.core.displaypub import publish_display_data
@@ -62,11 +62,15 @@ def __str__(self):
6262

6363
def loadmat(fname):
6464
"""
65-
Use pytables to read a variable from a v7.3 mat file
65+
Use h5py to read a variable from a v7.3 mat file
6666
"""
6767

68-
f = tables.File(fname)
69-
return f.root.__getattr__(f.root.__members__[0])
68+
f = h5py.File(fname)
69+
data = f.values()[0][:]
70+
if len(data.dtype) > 0:
71+
# must be complex data
72+
data = data['real'] + 1j * data['imag']
73+
return data
7074

7175

7276
def matlab_converter(matlab, key):

0 commit comments

Comments
 (0)