How to ignore not serializable variables when saving workspace
12 ビュー (過去 30 日間)
古いコメントを表示
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
0 件のコメント
回答 (2 件)
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.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Environment and Settings についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!