MATLAB Examples

clearempty

This function clears all empty variables from the current workspace.

Contents

Syntax

clearempty

Description

clearempty clears all empty variables from the current workspace.

Example

For this example we will create some variables, use whos to look at their respective sizes, and then we'll run clearempty.

a = 1;
b = pi;
c = [];
d = {};
e = '';
f = 'potatoes';
g = 1:10;
h = rand(100);
i = i;
j = peaks;
k = imread('eight.tif');
l = zeros(25);
m = ones(30); m(:)=[];

whos
  Name        Size             Bytes  Class     Attributes

  a           1x1                  8  double              
  b           1x1                  8  double              
  c           0x0                  0  double              
  d           0x0                  0  cell                
  e           0x0                  0  char                
  f           1x8                 16  char                
  g           1x10                80  double              
  h         100x100            80000  double              
  i           1x1                 16  double    complex   
  j          49x49             19208  double              
  k         242x308            74536  uint8               
  l          25x25              5000  double              
  m           0x0                  0  double              

Okay, it looks like c, d, e, and m are empty. Let's clear them in one fell swoop:

clearempty

whos
  Name        Size             Bytes  Class     Attributes

  a           1x1                  8  double              
  b           1x1                  8  double              
  f           1x8                 16  char                
  g           1x10                80  double              
  h         100x100            80000  double              
  i           1x1                 16  double    complex   
  j          49x49             19208  double              
  k         242x308            74536  uint8               
  l          25x25              5000  double              

Author Info

Version 1 of this function was written by Chad A. Greene of the University of Texas at Austin's Institute for Geophysics on June 25, 2014. Immediately upon uploading to the Mathworks File Exchange site, Jan Simon offered this cleaner, more robust rewrite of the function. Thanks Jan.