Thread Subject: save() calls saveobj method twice?

Subject: save() calls saveobj method twice?

From: Andrew Gartland

Date: 27 Jul, 2010 20:28:21

Message: 1 of 2

Hi,
I have made a class that inherits from handle that I want to be able to save to a mat file. It has other custom classes in it so i need to overide the saveobj method. I've done this and about half the time on one computer and all the time on a different computer the class saves and loads flawlessly using save(filename, object); from the prompt.

Sometimes the save operation fails. When it does it reports:
??? Error using ==> save
Error closing file F:/rf/sb/091006B.mat.

I've done some digging around to try and find the problem. I can catch the error with a try...catch. If I call the obj.saveobj() method directly instead of calling save(fn, obj) I get back a struct that looks fine and can be saved using save('temp.mat',tempStruct);

One weird thing I've noticed is that my saveobj() method seems to get called twice when i call save() on my object. It only fails on the second call to the saveobj() method. Why is it called twice? Is this related to my problem?

Since I can only catch an error when using save() but not saveobj() method, it does not appear that the error is in my code but rather in save(). Does anyone agree/disagree with this? Has anyone experienced similar problems. Thanks for the help. Please email me. Thanks!

Andrew

Subject: save() calls saveobj method twice?

From: Jonas

Date: 13 Jul, 2011 13:07:10

Message: 2 of 2

> One weird thing I've noticed is that my saveobj() method seems to get called twice when i call save() on my object. It only fails on the second call to the saveobj() method. Why is it called twice? Is this related to my problem?

I have contacted MathWorks support about the "calling saveojb() twice" and they replied:
"We don't guarantee that saveobj is only called once during save. Our current save (pre-HDF5) MAT implementation requires us to compute the size of the data on disk before actually saving the data and this cause us to go through the save process twice. This doesn't happen with HDF5 format. So doing

save('delme1.mat','j','-v7.3')

will display the message only once."

So for your case, the reason while it never fails on the first call is that this is only for determining the size, while the second one is for actually saving. Does your object do any logging by any chance, so that it could increase in size before the second call?

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
classes Andrew Gartland 27 Jul, 2010 16:30:59
save Andrew Gartland 27 Jul, 2010 16:30:59
load Andrew Gartland 27 Jul, 2010 16:30:59
rssFeed for this Thread

Contact us at files@mathworks.com