Save Workspace to Struct

Version (1.56 KB) by Andres
This function allows to save all the variables from the current workspace into a struct array


Updated 19 Apr 2012

View License

Sometimes you need to save the variables from your base workspace, but using "save" function will have them all stored individually so if you reload them into a new workspace it could be a mess, and some variables could be overwritten.
With this function, you can save all of them into a struct array, and so they'll be nicely packaged and ready to be saved to a .mat file that, when reloaded, will be easy to identify.

c={'cell1', 'cell2', 'cell3'}

theworkspace =

b: [1x22 double]
c: {'cell1' 'cell2' 'cell3'}
d: 768
e: [3x3 logical]

Cite As

Andres (2023). Save Workspace to Struct (, MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2011a
Compatible with any release
Platform Compatibility
Windows macOS Linux
Find more on Workspace Variables and MAT-Files in Help Center and MATLAB Answers

Community Treasure Hunt

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

Start Hunting!
Version Published Release Notes

Two little corrections, thanks to Jan Simon, this way the variables saved are not necessarily from the base workspace but from the current (caller) workspace:
WSVARS = evalin('caller', 'who');
THEWORKSPACE.(WSVARS{wscon}) = thisvar;