Skip to main content
Engineering LibreTexts

4.4: The Workspace and Saving/Loading Data

  • Page ID
    55646
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    In the previous section, we created variables such as pi_approx and \(f l o a t c h e c k\). Where do these variables go? These variables - and all variables we, or the programs we launch from the command window, create - go to what is known as the "workspace." In some sense, the workspace is the part of the memory in Figure \(4.1\) (interpreted as the MATLAB environment) which has already been allocated. In order to see all the variables in our workspace we can do \(\gg>\) who. Note that \(\gg>\) who does not evaluate the variables and hence does not provide the values; rather, it provides a list of all the variable names to which we have assigned data. (To determine the value of a particular variable we would simply type \(\gg>\) variable.) Note that whos is the same as who but with additional information about size and data type (or "class") of each variable.

    If we wish to delete all the variables in the workspace, and start anew, we would do clear. (This of course only affects the MATLAB variables you have created, with no effect on the file system such as the .m files you might have created.) Alternatively, you might wish to just delete certain variables, say variable_ 1 and variable_ 2 , in which case we would do >> clear variable_ 1 variable_ 2 . For smaller programs memory management is not an issue, but for larger programs it is important to clear or more generally allocate and de-allocate memory carefully.

    It is often the case that we wish to save the results of our calculations for future use - rather than re-compute. This is done very simply in MATLAB : to save variables variable_ 1 and variable_2 to a data file save_for_later.mat we do save save_for_later variable_1 variable_2. To reload this data we simply do load save_for_later which will include the contents of save_for_later, in our case variable_1 and variable_2, in the current workspace - that is, in addition to the current contents of your workspace. Note the .mat file is a MATLAB specific data file which is the most convenient within MATLAB , but there are also many other (and more universal) formats in which data can be saved.


    This page titled 4.4: The Workspace and Saving/Loading Data is shared under a CC BY-NC-SA 4.0 license and was authored, remixed, and/or curated by Masayuki Yano, James Douglass Penn, George Konidaris, & Anthony T Patera (MIT OpenCourseWare) via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.