I am attempting to load a .mat
file containing a number of structures, each of which has numerous fields, using Python and Scipy.io and loadmat()
. When I attempt to load the entire file, I am met with a ValueError: no field of name
error.
I have attempted to load each top-level struct individually using the variable_names
parameter and have narrowed the issue down to those structs containing fields that include structs without a name associated.
Fields within the BottomTrack struct
The error I get is the following:
ValueError Traceback (most recent call last)
Cell In[70], line 4
1 # mat=spio.loadmat("C:/Users/mthom/Desktop/test.mat", variable_names=['Transformation_Matrices'], struct_as_record=True)# load mat-file
2 # mat=spio.loadmat("C:/Users/mthom/Desktop/test.mat")# load mat-file
3 # mat=spio.loadmat("C:/Users/mthom/Desktop/ADCP Files/01-Transect_test2.mat")
----> 4 mat=spio.loadmat("C:/Users/mthom/Desktop/ADCP Files/test.mat", variable_names=['WaterTrack'])
File c:Usersmthomanaconda3envscs7643-projectlibsite-packagesscipyiomatlab_mio.py:226, in loadmat(file_name, mdict, appendmat, **kwargs)
224 with _open_file_context(file_name, appendmat) as f:
225 MR, _ = mat_reader_factory(f, **kwargs)
--> 226 matfile_dict = MR.get_variables(variable_names)
228 if mdict is not None:
229 mdict.update(matfile_dict)
File c:Usersmthomanaconda3envscs7643-projectlibsite-packagesscipyiomatlab_mio5.py:332, in MatFile5Reader.get_variables(self, variable_names)
330 continue
331 try:
--> 332 res = self.read_var_array(hdr, process)
333 except MatReadError as err:
334 warnings.warn(
335 'Unreadable variable "%s", because "%s"' %
336 (name, err),
337 Warning, stacklevel=2)
...
File _mio5_utils.pyx:719, in scipy.io.matlab._mio5_utils.VarReader5.array_from_header()
File _mio5_utils.pyx:963, in scipy.io.matlab._mio5_utils.VarReader5.read_struct()
ValueError: no field of name
Is there any way of dealing with this? Alternatively, is there a way to load only specific structure fields using loadmat()
, so I can still access a specific field, say ‘Sample_Number’, within the above structure example without running into this error?
EDIT:
I am unfamiliar with MatLAB files, so excuse my incorrect terminology. The file I have is generated by an instrument, so I am unsure as to how to recreate this. I would like to load the values in a specific field (BT_Vel) that is within one workspace (BottomTrack) within the file. I have no issue loading other workspaces and parsing the data within each field, however I am finding that when the workspace includes fields that are structs, like shown in the screenshot linked above, where the struct includes a field showing no name, I get an error and am unable to load anything from that workspace. When I inspect the field in MatLAB, it says ‘The variable…does not exist’.
Variable Does Not Exist
Matt Thomforde is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
5