Convert MAT file to v7.3

62 views (last 30 days)
Brent Justice
Brent Justice on 17 Sep 2021
Edited: Stephen23 on 17 Sep 2021
I have 2 questions:
1) The latest version of the MAT file is v7.3. Are all older MAT file versions able to be converted to a v7.3 file losslessly? i.e. does v7.3 support all datatypes/metadata supported by previous file formats?
2) What is the best way to convert old MAT file formats to v7.3 MAT files?
It seems like the following code snippet works pretty well for converting old MAT file formats to v7.3:
clear;
load('OldVersion.mat');
save('NewVersion.mat','-v7.3','-nocompression');
Is there any situation where this code snippet would fail for some reason?
Thanks everyone. If you want context for these questions, I've been asked to ingest MAT files into a data pipeline. The MAT files that I'm getting are v4 - v7.3. I've discovered that the latest v7.3 MAT files are HDF5 compliant files. Rather than adjust our (non-MATLAB) data pipeline to be able to handle all MAT file versions, it seems easier to upgrade incoming MAT files to v7.3 and just to use our well supported/documented HDF5 library for ingestion.
  3 Comments
Brent Justice
Brent Justice on 17 Sep 2021
In my use-case, I'm spooling up a MATLAB thread entirely to perform this conversion, so spamming the workspace should be a non-issue.
I do see how you recommendation is cleaner though.
I think the bigger issue that I don't necessarily like about this approach is that is forcibly loads the entire MAT file into memory before performing the conversion. This is going to be a pain for 10GB MAT files. I might try to look at interating through all the variables in a MAT file and 1-by-1 loading these into memory and then saving to the NewVersion.MAT
Stephen23
Stephen23 on 17 Sep 2021
You can load and save the individual (or any selected) variables using the structure approach too.

Sign in to comment.

Answers (1)

Stephen23
Stephen23 on 17 Sep 2021
Edited: Stephen23 on 17 Sep 2021
You can load and save the individual (or any selected) variables using the structure approach too. Lets try it:
A = 1:3;
B = 4:6;
C = 7:9;
save('old.mat','A','B','C','-v4')
clearvars
W = whos('-file','old.mat');
W.name
ans = 'A'
ans = 'B'
ans = 'C'
S = load('old.mat',W(1).name);
save('new.mat','-struct','S','-v7.3','-nocompression')
for k = 2:numel(W)
S = load('old.mat',W(k).name);
save('new.mat','-struct','S','-append','-nocompression')
end
Checking:
type new.mat
MATLAB 7.3 MAT-file, Platform: GLNXA64, Created on: Fri Sep 17 18:40:52 2021 HDF5 schema 1.00 . IMHDFÿÿÿÿÿÿÿÿà ÿÿÿÿÿÿÿÿ`¨¨TREEÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿHHEAPX ÈABC8( @4 ð?4áDaMATLAB_classdouble@SNO ¸( @4 @@4áDaMATLAB_classdouble@( @4 @ @"4áDaMATLAB_classdouble@
whos('-file','new.mat').name
ans = 'A'
ans = 'B'
ans = 'C'

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!