Pages

17.4.12

Save Workspace to Struct

This was a nice function that I created today, and it allows to save all the variables from the "base" workspace to a single struct, in order to have them all nicely packed in case you want to create mat file with them and later reload it without having so much worry of overwriting the variables in the other workspace. I gratefully accept suggestions for better options.
And now it's also available at the Matlab Central (if you want to rate it, which is always welcome):  http://www.mathworks.com/matlabcentral/fileexchange/36257-save-workspace-to-struct

%This Script saves all the variables from the current workspace into a
%single structure array.
%Created by Andres Gonzalez. 2012

function WStruct=ws2struct()

WSVARS = evalin('base', 'who');
for wscon=1:size(WSVARS,1)
    thisvar=evalin('base', WSVARS{wscon});
    eval(strcat('THEWORKSPACE.(WSVARS{wscon})=thisvar;'))
end

WStruct=THEWORKSPACE;

No comments:

Post a Comment