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 个评论
回答(2 个)
dpb
2023-9-3
编辑:dpb
2023-9-3
unzip ExampleFile
dir
whos -file ExampleFile _*
%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*
S=load('NewFile','xA*')
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
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
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
2023-9-3
Use @James Tursa https://www.mathworks.com/matlabcentral/fileexchange/42274-loadfixnames-loads-a-mat-file-fixing-invalid-names?s_tid=srchtitle which is designed for these kind of cases.
3 个评论
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
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 Center 和 File Exchange 中查找有关 Workspace Variables and MAT Files 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!