Clear Filters
Clear Filters

why matab show "because it appears to be corrupt"

19 views (last 30 days)
yiping sun
yiping sun on 19 Oct 2021
Edited: DGM on 22 Apr 2024
so i want to save a big value and use v7.3, but it shows this:Unable to write to file 'D:\matlab\test.mat' because it appears to be
corrupt.
i also change the preference to save -v7.3, but still it doesn't work.

Answers (1)

Rishav
Rishav on 22 Feb 2024
Hi yiping,
It is possible that you are using 'savefig' and 'saveas' functions, and these functions do not support the large size *.mat files.
You can try using 'save' function directly with the '-v7.3' flag argument as mentioned in the MATLAB documentation: https://www.mathworks.com/help/releases/R2021a/matlab/ref/save.html
Here is one example:
d = rand(10000,10000);
f = figure;
l = plot(d);
save('bigbigfig.mat', 'f', '-v7.3');
Use 'open' to read the *.mat file and then use 'gcf' to get the figure.
  5 Comments
DGM
DGM on 21 Apr 2024
Edited: DGM on 21 Apr 2024
I think I'm with @Dyuman Joshi here. While using saveas() or savefig() might cause errors, I don't see a workflow where you could be misusing either to produce such an error message. Let's assume that the intent was to do something like this:
%% the model workflow
clc; clf; clearvars
% save a normal .mat file
A = rand(100);
save('test.mat','A','-v7.3')
% append to the file using save()
% this does throw a warning about the unnecessary format specification
% but the question says that's how it's being called
B = fliplr(A);
save('test.mat','B','-append','-v7.3')
Instead of using save(), we could misuse figure tools or .fig files in different ways in an attempt to replicate the error.
%% the wrong tools are used to begin with
clc; clf; clearvars
% save a misnamed .fig file
% this will fail with either saveas() or savefig()
% since neither will allow the creation of a file with a .mat extension
A = rand(100);
savefig(gcf,'test.mat')
% append to a misnamed .fig file as if it were a plain .mat file
% this never gets reached
B = fliplr(A);
save('test.mat','B','-append','-v7.3')
%% the wrong tools are used for appending
clc; clf; clearvars
% save a normal .mat file
A = rand(100);
save('test.mat','A','-v7.3')
% attempt to append to the file with either saveas() or savefig()
% the question says we're using the syntax for save()
% so this will throw an error about the number of arguments
B = fliplr(A);
savefig('test.mat','B','-append','-v7.3')
%% the wrong tools are used to begin with,
% but the file is modified externally
clc; clf; clearvars
% save a misnamed .fig file
% this doesn't throw an error
A = rand(100);
savefig(gcf,'test.fig')
movefile('test.fig','test.mat')
% append to a misnamed .fig file as if it were a plain .mat file
% this doesn't throw an error
% but the .mat file will not contain the expected content
% instead, it will contain B, two structs, and a figure will be opened.
B = fliplr(A);
save('test.mat','B','-append','-v7.3')
% clear everything and try to load the file
clc; clearvars; close all % no figures are open here
load test.mat % opens a figure
All of those fail in one way or another, but not in the way that's described.
Let's ignore the potential misuse of saveas()/savefig() and go back to the model workflow. Can we do something else which causes this sort of error with what is ostensibly a normal .mat file, using save()?
%% what if the original file were not -v7.3?
clc; clf; clearvars
% save an old .mat file
A = rand(100);
save('test.mat','A','-v4')
% append to the file using save()
% this does throw a warning about the unnecessary format specification
% but the question says that's how it's being called
B = fliplr(A);
save('test.mat','B','-append','-v7.3')
So that doesn't produce any errors.
%% what if the original file were empty?
clc; clf; clearvars
% save an old .mat file
% since -v4 doesn't support uint8 arrays
% this will create a .mat file containing no variables
% but it will also throw a warning explaining what happened
A = im2uint8(rand(100));
save('test.mat','A','-v4')
% append to the file using save()
% this will throw an error about the file being corrupt
B = fliplr(A);
save('test.mat','B','-append','-v7.3')
This does produce the error described. I don't know if there are other, more plausible ways of creating an empty .mat file, but type-dependent exclusion could be one possibility. Still, this would throw warnings which are fairly obvious and explanatory.
Can we find another way to write an invalid .mat file without throwing any warnings during the initial write?
%% what if the original file were simply not a .mat file?
clc; clf; clearvars
% simply use anything to write a file with an inappropriate extension
% neither of these two tools will complain
A = im2uint8(rand(100));
%imwrite(A,'test.mat','jpg')
csvwrite('test.mat',A)
% append to the file using save()
% this will throw an error about the file being corrupt
B = fliplr(A);
save('test.mat','B','-append','-v7.3')
... and of course, there are any number of ways to either inappropriately rename or modify a file externally in order to create something that can't be appended to.
It's only implied that we're appending to an existing file. What if we're trying to write a new file? Could we perhaps get this error by trying to write to a write-protected location? No; we'd get a "permission denied" error. Could we get this error by writing to an invalid path? No; we'd get a "[path] does not exist" error.
I'm probably missing something, but that's my thoughts.
DGM
DGM on 22 Apr 2024
Edited: DGM on 22 Apr 2024
I remembered an old thread that made me think of another case that could cause a similar error message. In this case, we start with a v4 .mat file containing a VAX G float array.
% append to an old file using save()
% this throws the same warning about the unnecessary format specification
% no error is thrown
B = rand(100);
save('test_v4_vaxg.mat','B','-append','-v7.3')
Warning: Ignoring the version specified. The version flag is not required when using the '-append' flag.
% loading the file fails with a "file might be corrupt" error
% this fails in any version made within the last ~16 years or so
% the problem in this case isn't appending to the file.
% the original .mat file was written using format options
% which are simply no longer supported
% i have no idea if the appended file is even valid anymore
clc; clearvars
load test_v4_vaxg.mat
Error using load
Unable to read MAT-file /users/mss.system.lhztm/test_v4_vaxg.mat. File might be corrupt.
This is perhaps a unique case, as the .mat file is indeed a valid .mat file, and while the original content can't be decoded by MATLAB, the file can be appended without error (or at least we know that it appears to append successfully).
Of course, that also means this isn't a plausible explanation, as OP received a "file might be corrupt" error during write, not during read. Are there other once-valid .mat file options which have become unsupported and would cause a failure to append? I don't know.

Sign in to comment.

Categories

Find more on Install Products in Help Center and File Exchange

Tags

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!