How to ignore not serializable variables when saving workspace

조회 수: 12 (최근 30일)
Mech Princess
Mech Princess 2018년 11월 27일
편집: raym 2020년 2월 25일
Hi.
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$).');
Thanks

답변 (2개)

Guillaume
Guillaume 2018년 11월 27일
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))
save('somefile.txt', varsnojava.name)
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
raym 2020년 2월 25일
편집: raym 2020년 2월 25일

카테고리

Help CenterFile Exchange에서 Workspace Variables and MAT-Files에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by