error when saving: unable to write to MAT-file, File may be corrupt

2867 views
Skip to first unread message

Evi Daems

unread,
Mar 5, 2009, 1:05:04 PM3/5/09
to
Hi,

Sometimes, when I try to save a matrix into a mat-file (save FileName Matrix ), I get the error: Unable to write to MAT-file FileName, File may be corrupt. When I try it a few times, sometimes it works, sometimes it fails. First I thought it has something to do with the fact that the mat-file already existed, but also for new mat-files, it sometimes fails.
Has anybody have an idea how to solve this?

Thanks!
Evi

JoeyB

unread,
Apr 8, 2009, 9:09:09 AM4/8/09
to

I'm having the same problem. This first attempt at save fails. The
next works:
??? Error using ==> save

Unable to write file
C:\Work\AmbientCorrection\PLT_658_1ALN6412_WOT.mat:
permission denied.

Pouria

unread,
Mar 2, 2010, 5:34:04 PM3/2/10
to
i am also experiencing the same problem, on a random basis (i think, unless there is a solid reason) i get the unable to write to MAT-file error msg. sometimes it is a new file and sometimes when trying to rewrite the same file.

any information or discovery is much appreciated
i use Matlab 2008a(7.6.0.324)

Thanks

-P

JoeyB <joseph...@gm.com> wrote in message <385f884f-927d-446b...@z14g2000yqa.googlegroups.com>...


> On Mar 5, 2:05=A0pm, "Evi Daems" <evi_da...@hotmail.com> wrote:
> > Hi,
> >

> > Sometimes, when I try to save a matrix into a mat-file (save FileName Mat=
> rix ), I get the error: Unable to write to MAT-file FileName, File may be c=
> orrupt. When I try it a few times, sometimes it works, sometimes it fails. =
> First I thought it has something to do with the fact that the mat-file alre=


> ady existed, but also for new mat-files, it sometimes fails.
> > Has anybody have an idea how to solve this?
> >
> > Thanks!
> > Evi
>
> I'm having the same problem. This first attempt at save fails. The
> next works:

> ??? Error using =3D=3D> save

Andrew Fiedler

unread,
May 13, 2010, 9:52:04 AM5/13/10
to
I'm having the same problem with MATLAB R2009b (7.9.0.529). I seems completely random when it can save mat files and when it won't. Saving files to CSV in the same folder location works, and is a work-around for now but I have many programs that are already written to save MAT files which seem to fail for some inexplicable reason. Also, deleting the existing MAT files before trying to save new copies does not help.

"Pouria " <p.mo...@gmail.com> wrote in message <hmk3os$q5s$1...@fred.mathworks.com>...

Steven Lord

unread,
May 13, 2010, 10:30:49 AM5/13/10
to

"Andrew Fiedler" <afie...@gmail.com> wrote in message
news:hsh064$kbr$1...@fred.mathworks.com...

> I'm having the same problem with MATLAB R2009b (7.9.0.529). I seems
> completely random when it can save mat files and when it won't. Saving
> files to CSV in the same folder location works, and is a work-around for
> now but I have many programs that are already written to save MAT files
> which seem to fail for some inexplicable reason. Also, deleting the
> existing MAT files before trying to save new copies does not help.

Please contact Technical Support and work with them to determine the
circumstances that cause this behavior so that if the problem is in MATLAB
that we can correct it and if the problem is in some aspect of your
configuration that you can take the appropriate action.

--
Steve Lord
sl...@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ


Peter

unread,
Jul 14, 2010, 7:31:04 AM7/14/10
to
"Andrew Fiedler" <afie...@gmail.com> wrote in message <hsh064$kbr$1...@fred.mathworks.com>...

> I'm having the same problem with MATLAB R2009b (7.9.0.529). I seems completely random when it can save mat files and when it won't. Saving files to CSV in the same folder location works, and is a work-around for now but I have many programs that are already written to save MAT files which seem to fail for some inexplicable reason. Also, deleting the existing MAT files before trying to save new copies does not help.

<skip>

Andrew, I experience the same problem. It seems random, and occurs with code which I haven't changed lately. Has Technical Support been able to help you out? Any feedback is appreciated.

Peter

Peter

unread,
Jul 14, 2010, 7:33:06 AM7/14/10
to
"Andrew Fiedler" <afie...@gmail.com> wrote in message <hsh064$kbr$1...@fred.mathworks.com>...
> I'm having the same problem with MATLAB R2009b (7.9.0.529). I seems completely random when it can save mat files and when it won't. Saving files to CSV in the same folder location works, and is a work-around for now but I have many programs that are already written to save MAT files which seem to fail for some inexplicable reason. Also, deleting the existing MAT files before trying to save new copies does not help.

<skip>

Maurizio De Pitta'

unread,
Aug 20, 2010, 10:20:04 AM8/20/10
to
Hi all,
indeed the same problem occurs also both on my laptop and PC (using MATLAB R2009a version 7.8.0.347) apparently essentially on random bases. The only note that I can make is that saving is still possbile through the GUI, but not from command line.

It usually happens after I have had matlab open for a while and very often when I resume from hibernation/Suspend my computer.

I need to quit and restart.
OSs are WinXP Pro SP2 and Vista 2008, Pro SP2.

If anyone does have any hint, is very welcome.
Thanks.
Maurizio De Pitta'.

Maurizio De Pitta'

unread,
Aug 21, 2010, 4:47:05 AM8/21/10
to

Sven

unread,
Sep 24, 2010, 4:29:06 PM9/24/10
to
"Maurizio De Pitta'" <maurizi...@email.it> wrote in message <i4o3q9$ita$1...@fred.mathworks.com>...
I experience the same problem when finishing a Stateflow code generation.
Is the problem already solved by the technical support? I couldn't even find a bug report for this issue.
On my computer is also Microsoft Access installed, which is registering the file extension .mat for its tables. Could the problem be an interference with Access?
Do the other reporters of this issue also use Microsoft Access on their computers?
Please let me know if it is worth to further investigate in this hypothesis!

Maurizio De Pitta'

unread,
Oct 1, 2010, 5:26:06 AM10/1/10
to
Dear all,
has anyone found a solution meanwhile, to this problem?

It is now occurring on my MATLAB (version 7.8.0.347 R2009a) almost on daily basis. So far the cases when it occurs are:
- after MATLAB session has been open for a while (1 hour), even wihtout having saved anything before, any saving action is vanish.
- multiple MATLAB sessions are open and I am working with files in the same directory (this indeed might conflict with VISTA Admin rights handling)
- running routines / batch jobs that sequentially call the same routine and at each run this latter needs save data in a temporary file

It is somehow interesting that if I use the GUI to save, I am able to save either data or figures. The problem seems mainly from command line. Everytime it does occur, I need to shut MALTAB down and restart it.

Please help. This problem is now getting extremely irritating and seriously slowing down my work.

Best,
Maurizio.

Steven_Lord

unread,
Oct 4, 2010, 11:41:02 AM10/4/10
to

"Maurizio De Pitta'" <maurizi...@email.it> wrote in message

news:i849fe$284$1...@fred.mathworks.com...

You should contact Technical Support for assistance in determining the cause
of this problem.

--
Steve Lord
sl...@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ

To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Sven

unread,
Oct 5, 2010, 5:56:06 AM10/5/10
to
For my code generation issue it helped to move the location of the mat file to a local drive instead of the former network share.

John

unread,
Apr 23, 2012, 11:28:06 AM4/23/12
to
All,

I get an error like this except mine complains it can't write the *.mat to the current directory because there is "No such file or directory" (kind of a catch 22). I'm calling the "save" command in a MATLAB Function block of a Simulink model.

I found if I ran "clear all" at the command line before each run of the simulation that the problem went away. I'm not sure what magic is going on (clear all doesn't erase *.mat files), but understanding is sometimes overrated. I hope this helps someone.

Have a great day.
John

K E

unread,
Apr 23, 2012, 12:10:06 PM4/23/12
to
I don't know if it is related, but I was getting an error on 'load' when it tried to load a mat file containing some Simulink output structures,
http://www.mathworks.com/matlabcentral/answers/35648-loading-structure-with-simulink-output-returns-timeseries-object-error
Try saving the mat file without any Simulink-specific variables and see if you still get the save error.

Maarten Jansen

unread,
Mar 13, 2014, 6:17:10 AM3/13/14
to
I have the same problem and did not find a real solution.
As a last resort I constructed a try-catch solution with a retry:

% Ugly try-catch solution to ugly problem...
saved = 0;
while saved == 0;
try
disp(['Attempting to save file:' filename]);
save(filename);
saved = 1;
catch
warning('Problem saving, retry saving...')
pause(.5);
end;
end;

pooja.c...@gmail.com

unread,
Jun 26, 2014, 7:50:29 AM6/26/14
to
I had the very same problem. Save would fail with an error saying, 'Write failure'. When I examined the array I was trying to save, it would be incompletely written, suspended in between. You can see how much has been written if you choose to save as an ascii file, you can't if you choose to save as a 'mat'.

The issue was with the disk being full. There was no space to write the array. :D Quite funny but really did tick me off when it happened. Was in the middle of something really important and I started thinking 'what will I do now!????' Gosh!! :D

Antonio Mathworks

unread,
Oct 28, 2015, 11:23:09 AM10/28/15
to
"Evi Daems" <evi_...@hotmail.com> wrote in message <gop48g$cnk$1...@fred.mathworks.com>...
Team, this is actually quite simple:
1) Add your folder under your SET PATH
2) Type the path+fileName when using load or save commands

E.g.
1) I added the folder C:\ImTheBest under SET PATH
2) save('C:\ImTheBest\MyMatFile.mat', 'var1')
3) load('C:\ImTheBest\MyMatFile.mat', 'var1')

Please accept my answer formally.

patel shital

unread,
Jun 27, 2016, 4:02:10 AM6/27/16
to
"Evi Daems" <evi_...@hotmail.com> wrote in message <gop48g$cnk$1...@fred.mathworks.com>...

Elena Syerko

unread,
Dec 15, 2016, 12:52:08 PM12/15/16
to
A possible reason to that can be that the data are too large to be written by a standard way into a .mat file. For the variables that exceed 2Gb in size you should use:

save('filename.mat','var_name', '-v7.3')

For more détails, see:

https://fr.mathworks.com/help/matlab/import_export/mat-file-versions.html

Sincerely


"patel shital" wrote in message <nkqmhs$m52$1...@newscl01ah.mathworks.com>...

Danylyna Shpakivska

unread,
Aug 20, 2022, 8:02:35 AMAug 20
to
Try reducing the length of the filename, it worked for me. In my case the data were not too large (0.12 GB) so it was not the problem.
Reply all
Reply to author
Forward
0 new messages