Modify invalid field names in .mat file using code

13 次查看(过去 30 天)
Hi, I have exported a .mat file from a program called Wintax4 and some of the field names are starting with _xxx which is not allowed by .mat file. This means I can't load my .mat file using data = load(data.mat) because it throws an error for invalid field name. How can I go about automatically going through this .mat file and just removing the underscore from all field names that start with underscore?
An example when I use data = Load('data.mat') I get the following error
"Error using load
Invalid field name: '_ADU_Odometer'."
There are about 10-15 field names using this underscore as the first letter and I have tons of these datalogs so manually editing them is not an option.
Thanks in advance

回答(2 个)

dpb
dpb 2023-9-3
编辑:dpb 2023-9-3
unzip ExampleFile
dir
. .. ExampleFile.mat ExampleFile.zip
whos -file ExampleFile _*
Name Size Bytes Class Attributes _ADU_Odometer 26087x1 208696 double _ADU_ResettableOdometer 26087x1 208696 double _ADU_TrackBeacon 260861x1 2086888 double _ADU_TrackDistance 52173x1 417384 double _ADU_TrackLap 52173x1 417384 double _ADU_TrackLapTime 260861x1 2086888 double _GPS_Latitude 52173x1 417384 double _GPS_Longitude 52173x1 417384 double _GPS_Speed 52173x1 417384 double
%S=load('ExampleFile') % _ADU_Odometer
fid=fopen('ExampleFile.mat','r');
[filename,~,~,encoding] = fopen(fid);
f=fread(fid,'*char*1').';
%whos f
%ix=strfind(f,'_ADU_')
%f(ix(1):ix(1)+20)
f=strrep(f,'_ADU_','xADU_');
%f(ix(1):ix(1)+20)
fid=fclose(fid);
fid=fopen('NewFile.mat','w','n',encoding);
fwrite(fid,f);
fid=fclose(fid);
whos -file NewFile x*
Name Size Bytes Class Attributes xADU_Odometer 26087x1 208696 double xADU_ResettableOdometer 26087x1 208696 double xADU_TrackBeacon 260861x1 2086888 double xADU_TrackDistance 52173x1 417384 double xADU_TrackLap 52173x1 417384 double xADU_TrackLapTime 260861x1 2086888 double
S=load('NewFile','xA*')
S = struct with fields:
xADU_Odometer: [26087×1 double] xADU_ResettableOdometer: [26087×1 double] xADU_TrackBeacon: [260861×1 double] xADU_TrackDistance: [52173×1 double] xADU_TrackLap: [52173×1 double] xADU_TrackLapTime: [260861×1 double]
Well, it does seem to work! I wasn't sure if there were some other details in the header that might have to be munged on as well besides just changing the names.
You'll have to do the other variables besides _ADU_*, too, of course, before you write the new one out; I was just seeing if it would work or not.
  4 个评论
James Tursa
James Tursa 2023-9-18
True. Follow-up, the reason for this is that all of the variable and field name lengths are embedded within the file. Technically, if one could find where the length for a particular name is recorded in the file you could change it and then change the name's length in your rewrite, but it is much easier to just keep the length the same.
dpb
dpb 2023-9-18
编辑:dpb 2023-9-19
@Walter Roberson already pointed out the problem, I'll just note the difference in
f=strrep(f,'_GPS_','GPS_');
and what I did above in
f=strrep(f,'_ADU_','xADU_');
is that I simply substituted an x in place of the leading underscore; I didn't remove it. That would ensure no two variable names would end up the same and let one have a decent shot of finding all that had been modified by searching for the leading 'x' to be able to check had a set of names wanted...they could then be turned into a set of "real" variables as desired for actual use.

请先登录,再进行评论。


Walter Roberson
Walter Roberson 2023-9-3
  3 个评论
Walter Roberson
Walter Roberson 2023-9-18
I tried it earlier this year and it seemed to work, except that for the particular file I was using it on, it told me that there were too many variables to fix up.
On that particular file, I found that the valid variablenames out of it would load properly and without message if I used load as a command, but if I used load as a function it would warn about invalid names.
It might make a difference that it was struct field names rather than the names of the variables themselves.
dpb
dpb 2023-9-18
I guess that would depend upon whether it compresses the content of the whole file or just compresses the data within each variable before writing it.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Workspace Variables and MAT Files 的更多信息

产品


版本

R2023a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by