I have a file in Matlab from which I can import a struct:
>> load bla % imports a struct called G >> G G = Inp: [40 x40x2016 uint8] Tgt: [8 x2016 double] Ltr: [1 x2016 double] Relevant: [1 2 3 4 5 6 7 8]
Now I want to do the same in Python:
x = scipy.io.loadmat('bla.mat')
>>> x
{'__version__': '1.0', '__header__': 'MATLAB 5.0 MAT-file, Platform: PCWIN, Created on: Wed Jun 07 21:17:24 2006', 'G': array([[<scipy.io.matlab.mio5.mat_struct object at 0x0191F230>]], dtype=object), '__globals__': []}
>>> x['G']
array([[<scipy.io.matlab.mio5.mat_struct object at 0x0191F230>]], dtype=object)
>>> G = x['G']
>>> G
array([[<scipy.io.matlab.mio5.mat_struct object at 0x0191F230>]], dtype=object)
Get the filename for an example .mat file that contains a MATLAB struct called teststruct and load the contents.,Get the filename for an example .mat file from the tests/data directory.,By default SciPy reads MATLAB structs as structured NumPy arrays where the dtype fields are of type object and the names correspond to the MATLAB struct field names. This can be disabled by setting the optional argument struct_as_record=False.,Get the ‘stringfield’ of the first element in the MATLAB struct.
>>> from os.path
import dirname, join as pjoin
>>>
import scipy.io as sio
>>> data_dir = pjoin(dirname(sio.__file__), 'matlab', 'tests', 'data') >>>
mat_fname = pjoin(data_dir, 'testdouble_7.4_GLNX86.mat')
>>> mat_contents = sio.loadmat(mat_fname)
>>> sorted(mat_contents.keys())['__globals__', '__header__', '__version__', 'testdouble'] >>>
mat_contents['testdouble']
array([
[0., 0.78539816, 1.57079633, 2.35619449, 3.14159265,
3.92699082, 4.71238898, 5.49778714, 6.28318531
]
])
>>> matstruct_fname = pjoin(data_dir, 'teststruct_7.4_GLNX86.mat') >>>
matstruct_contents = sio.loadmat(matstruct_fname) >>>
teststruct = matstruct_contents['teststruct'] >>>
teststruct.dtype
dtype([('stringfield', 'O'), ('doublefield', 'O'), ('complexfield', 'O')])
>>> teststruct.size 1 >>> teststruct.shape(1, 1)
Following this question which asks (and answers) how to read .mat files that were created in Matlab using Scipy, I want to know how to access the fields in the imported structs.,I have a file in Matlab from which I can import a struct:,Python – How to install packages using pip according to the requirements.txt file from a local directory,The question is, how can I access the members of the struct G: Inp, Tgt, Ltr and Relevant, the way I can in Matlab?
I have a file in Matlab from which I can import a struct:
>> load bla % imports a struct called G >> G G = Inp: [40 x40x2016 uint8] Tgt: [8 x2016 double] Ltr: [1 x2016 double] Relevant: [1 2 3 4 5 6 7 8]
Now I want to do the same in Python:
x = scipy.io.loadmat('bla.mat')
>>> x
{'__version__': '1.0', '__header__': 'MATLAB 5.0 MAT-file, Platform: PCWIN, Created on: Wed Jun 07 21:17:24 2006', 'G': array([[<scipy.io.matlab.mio5.mat_struct object at 0x0191F230>]], dtype=object), '__globals__': []}
>>> x['G']
array([[<scipy.io.matlab.mio5.mat_struct object at 0x0191F230>]], dtype=object)
>>> G = x['G']
>>> G
array([[<scipy.io.matlab.mio5.mat_struct object at 0x0191F230>]], dtype=object)
How To Access Fields In A Struct Imported From A Mat File Using Loadmat In Pyth, 1 week ago How to access fields in a struct imported from a .mat file using loadmat in Python? Viewed 10548 times Following this question which asks (and answers) how to read .mat files that were created in Matlab using Scipy, I want to know how to access the fields in the imported structs. ,Following this question which asks (and answers) how to read .mat files that were created in Matlab using Scipy, I want to know how to access the fields in the imported structs., 1 week ago Feb 19, 2018 · I have tried two different methods to import this structure into Python (v3.5): a) Import using the io module from Scipy library. import scipy.io as spio. lib = spio.loadmat ('lib.mat') which produces a dict in python called 'lib' whose fields are not accessible (at least I am not aware how to access, for example, the 32 structures listed in ...
>> load bla % imports a struct called G >> G G = Inp: [40 x40x2016 uint8] Tgt: [8 x2016 double] Ltr: [1 x2016 double] Relevant: [1 2 3 4 5 6 7 8]
>> load bla % imports a struct called G >> G G = Inp: [40 x40x2016 uint8] Tgt: [8 x2016 double] Ltr: [1 x2016 double] Relevant: [1 2 3 4 5 6 7 8]
x = scipy.io.loadmat('bla.mat') >>>x {'__version__': '1.0', '__header__': 'MATLAB 5.0 MAT-file, Platform: PCWIN, Created on: Wed Jun 07 21:17:24 2006', 'G': array([[<scipy.io.matlab.mio5.mat_struct object at 0x0191F230>]], dtype=object), '__globals__': []} >>>x['G'] array([[<scipy.io.matlab.mio5.mat_struct object at 0x0191F230>]], dtype=object) >>>G = x['G'] >>>G array([[<scipy.io.matlab.mio5.mat_struct object at 0x0191F230>]], dtype=object)
Official Scipy.io Documentation,If you don’t already have scipy, you can use the pip command to install the same
pip install scipy
from scipy.io
import loadmat
annots = loadmat('annotation_0001.mat')
print(annots)
{
'__header__': b 'MATLAB 5.0 MAT-file, Platform: PCWIN, Created on: Tue Dec 14 15:57:03 2004',
'__version__': '1.0',
'__globals__': [],
'box_coord': array([
[2, 300, 1, 260]
], dtype = uint16),
'obj_contour': array([
[37.16574586, 61.94475138, 89.47697974, 126.92081031,
169.32044199, 226.03683241, 259.07550645, 258.52486188,
203.46040516, 177.5801105, 147.84530387, 117.0092081,
1.37384899, 1.37384899, 7.98158379, 0.82320442,
16.2412523, 31.65930018, 38.81767956, 38.81767956
],
[58.59300184, 44.27624309, 23.90239411, 0.77532228,
2.97790055, 61.34622468, 126.87292818, 214.97605893,
267.83793738, 270.59116022, 298.67403315, 298.67403315,
187.99447514, 94.93554328, 90.53038674, 77.31491713,
62.44751381, 62.99815838, 56.94106814, 56.94106814
]
])
}
con_list = [
[element
for element in upperElement
]
for upperElement in annots['obj_contour']
]
[ [37.16574585635357, 61.94475138121544, 89.47697974217309, 126.92081031307546, 169.32044198895025, 226.03683241252295, 259.0755064456721, 258.52486187845295, 203.4604051565377, 177.58011049723754, 147.84530386740326, 117.0092081031307, 1.3738489871086301, 1.3738489871086301, 7.98158379373848, 0.8232044198894926, 16.24125230202577, 31.65930018416205, 38.81767955801104, 38.81767955801104], [58.59300184162066, 44.27624309392269, 23.90239410681403, 0.7753222836096256, 2.9779005524862328, 61.34622467771641, 126.87292817679563, 214.97605893186008, 267.83793738489874, 270.59116022099454, 298.6740331491713, 298.6740331491713, 187.9944751381216, 94.93554327808477, 90.53038674033152, 77.31491712707185, 62.44751381215474, 62.998158379373876, 56.94106813996319, 56.94106813996319] ]
import pandas as pd