Clear Filters
Clear Filters

How to ignore not serializable variables when saving workspace

16 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
not serializable
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$).');

Answers (2)

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 =; %a non-serialisable java object
>> save('test.mat') %note that specifying '-v7.3' leads to the same warning.
Warning: is not serializable
Warning: Variable 'socket' was not saved. Variables of type '' (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',, 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.

raym on 25 Feb 2020
Edited: raym on 25 Feb 2020

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!