Modify invalid field names in .mat file using code
14 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Dyuman Joshi
am 3 Sep. 2023
Please attach some of the .mat files. Use the paperclip button to do so.
Antworten (2)
dpb
am 3 Sep. 2023
Bearbeitet: dpb
am 3 Sep. 2023
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 Kommentare
James Tursa
am 18 Sep. 2023
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
am 18 Sep. 2023
Bearbeitet: dpb
am 19 Sep. 2023
@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
am 3 Sep. 2023
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 Kommentare
Walter Roberson
am 18 Sep. 2023
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
am 18 Sep. 2023
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.
Siehe auch
Kategorien
Mehr zu Workspace Variables and MAT Files finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!