How to ignore not serializable variables when saving workspace
13 views (last 30 days)
I am collecting data from external equipment which communicates with java. I need to save the workspace but it won't save anything to do with java stuff. So once it comes to that variable, it stops saving and gives the error
Error using save
My workspace has a lot of variables both serializable and not. Does anyone know how to save ignoring the variables not serializable (those communicating with java for example) without having to delete it manually or name them one by one?
Closet I have come is using the following, but it gives same error.
save('savename.mat', '-regexp', '^(?!s$).');
Guillaume on 27 Nov 2018
I get a warning not an error if I try to save a workspace with an non-serialisable java variable:
>> socket = java.net.DatagramSocket; %a non-serialisable java object
>> save('test.mat') %note that specifying '-v7.3' leads to the same warning.
Warning: java.net.DatagramSocket@54d46c8 is not serializable
Warning: Variable 'socket' was not saved. Variables of type 'java.net.DatagramSocket' (or one of its members) are not supported
for this MAT-file version. Use a newer MAT-file version.
The warning is a bit misleading, since it appears using the latest Mat-file version (7.3).
Anyway, if you want to exclude all the java variables from save, you could do this:
vars = whos;
varsnojava = vars(arrayun(@(var) ~isjava(evalin('base', var.name)), vars))
Even better would be to have all variables you want to save as fields of a single structure and simply save that structure (with the -struct option of save). Ultimately, this would be more robust as you wouldn't have to worry about any temporary, or non-serialisable, or any other undesired variable in your workspace when you do the save.