I want to use save function to save variables as a cache file cachefile.
When I do so, I receive this error message:
save(cachefile, 'Tbhv', 'rec', 'Ttrialcounts', 'obj');
Error using save
Unable to save file '\\**\**\*****.mat'. The file could not be closed, and might now be corrupt.
Among them, Tbhv and Ttrialcounts are table data, whereas rec and obj are objects of two different custom classes. They are pretty complicated.
Initially, the problem appeared to be related to obj. Because I could not save it alone
So I implemented saveobj and loadobj methods to the class; the properties of the obj are saved as struct by saveobj, and when loading, the struct is used to re-construct the obj by the static loadobj method. With that, the above line works without error.
Now all the three commands below work. Yet, I cannot save all of the four in one file.
And the following combinations work as well.
save(cachefile,'Tbhv', 'Ttrialcounts', 'rec')
save(cachefile,'Tbhv', 'Ttrialcounts', 'obj')
However, these combinations end up with an error.
How on earth each objects can be saved properly, but they cannot be saved together? Is it expected in some cases? Or is it a bug?
Futher digging this deeper, it appears to be related to the saveobj method of obj and rec.
Properties of obj look like this:
rec: [1×1 Record]
Tbhv: [126147×26 table]
Sbhv: [1×1 struct]
openEphysTimeBhv: [126147×1 double]
Tclu: [49×14 table]
Laser_t_s: [0×1 double]
Laser_onoff: [0×1 logical]
As you can see one of obj property holds rec, a Record object.
When I removed this, like
then the following worked.
OK, but why???