Consider the following Matlab code:
pmod(1).name{1} = 'regressor1';
pmod(1).param{1} = [1 2 4 5 6];
pmod(1).poly{1} = 1;
pmod(2).name{1} = 'regressor2-1';
pmod(2).param{1} = [1 3 5 7];
pmod(2).poly{1} = 1;
This creates a struct array. Each struct in the array contains three fields of type cell
. As such, we have the following hierarchy in pmod
:
pmod // struct array
|
*- struct
| |
| *- cell // contains 1 or more strings
| *- cell // contains 1 or more arrays
| *- cell // contains 1 or more arrays
|
*- struct [...]
I'm trying to use scipy.io
to produce the above data structures in Python, such that they can be loaded into Matlab (this hierarchy is required by SPM).
Creating a struct is straightforward, as scipy.io.savemat
saves any dict whose keys are all of type str
as a Matlab struct:
from scipy.io import savemat
struct = {
'field1': 1,
'field2': 2,
}
savemat('/tmp/p.mat', {'a_struct': struct})
However, when trying to generalize this to a struct array, I hit the following roadblock:
struct_array = [struct, struct]
savemat('/tmp/p.mat', {'s_array': struct_array})
This does not behave as expected; when loading p.mat
into Matlab, I get a 1x2 cell array, not a struct array.
scipy.io
?savemat('/tmp/p.mat', np.array(struct_array))
and savemat('/tmp/p.mat', np.array(struct_array, dtype=object))
, to no avail.To create an array of structures using the struct function, specify the field value arguments as cell arrays. Each cell array element is the value of the field in the corresponding structure array element. For code generation, corresponding fields in the structures must have the same type.
Structures store data in containers called fields, which you can then access by the names you specify. Use dot notation to create, assign, and access data in structure fields. If the value stored in a field is an array, then you can use array indexing to access elements of the array.
The Scipy.io (Input and Output) package provides a wide range of functions to work around with different format of files. Some of these formats are − Matlab. IDL.
Nested structures or structures containing a pointer to a structure are not supported. However, MATLAB can access an array of structures created in an external library.
You can use np.core.records.fromarrays
to construct a record array, which is roughly equivalent to a MATLAB struct, and will be converted to a MATLAB struct by scip.io.savemat
.
from numpy.core.records import fromarrays
from scipy.io import savemat
myrec = fromarrays([[1, 10], [2, 20]], names=['field1', 'field2'])
savemat('p.mat', {'myrec': myrec})
When opened in MATLAB, this gives:
>> load('p.mat')
>> myrec
myrec =
1x2 struct array with fields:
field1
field2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With