How to convert a large .mat file of (default format ~ 7) to a 7.3 MAT-file / HDF5 based format when the file is too large to load.

I have a program which exports .mat files in the default .mat format - I believe it v7 MAT-file? Due to the size I am unable to load the .mat file. When attempting to load the .mat file I receive an "Error using load. Cannot read file X". I have no control over the output of the program used to generate the .mat file. Is there a way in which the .mat file may be accessed to retrieve the content? Ideally I would like to retrieve the data and save it in a v7.3 MAT-file format. Thanks!

 Risposta accettata

-v7 format .mat files cannot have any component which is larger than 2 gigabytes. This suggests a strategy of using load() to request particular variables from the -v7 .mat file, and write the contents to the -v7.3 .mat file (possibly using matFile)

6 Commenti

Hi Walter, thanks for the extremely quick response!! I was incorrect. The matfiles are of the format 5.0. Can we request elements/vars from a 5.0 format .mat in the same manner?
Found it - page 1-4. https://www.mathworks.com/help/pdf_doc/matlab/matfile_format.pdf.
"5.0" format shows up for both -v5 and -v7 files, but not for -v7.3 files.
You can use whos to find the names of the variables, and then you can loop over the names with load()
in_matname = 'YourMatFileName.mat';
out_matname = 'NewMatFileName.mat';
S = whos('-file', in_matname);
for K = 1 : length(S)
thisvarname = S.name;
datastruct = load(in_matname, thisvarname);
save(out_matname, '-v7.3', '-struct', 'datastruct', '-append');
end
Excellent! Thanks for your help. Avid reader of your many posts!
I'm really new to matlab but I had to update the code like this to get it run:
in_matname = 'YourMatFileName.mat';
out_matname = 'NewMatFileName.mat';
S = whos('-file', in_matname);
for K = 1 : length(S)
thisvarname = S(K).name;
% disp(K);
% disp(thisvarname);
datastruct = load(in_matname, thisvarname);
if K == 1
save(out_matname, '-v7.3', '-struct', 'datastruct');
else
save(out_matname, '-struct', 'datastruct', '-append');
end
end

Accedi per commentare.

Più risposte (0)

Categorie

Community Treasure Hunt

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

Start Hunting!

Translated by