why matab show "because it appears to be corrupt"
30 ビュー (過去 30 日間)
古いコメントを表示
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.
0 件のコメント
回答 (1 件)
Rishav
2024 年 2 月 22 日
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 件のコメント
DGM
2024 年 4 月 21 日
編集済み: DGM
2024 年 4 月 21 日
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
2024 年 4 月 22 日
編集済み: DGM
2024 年 4 月 22 日
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')
% 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
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.
参考
カテゴリ
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!