Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Clear Empty Variables in Workspace

Subject: Clear Empty Variables in Workspace

From: Eric

Date: 20 Mar, 2009 13:13:01

Message: 1 of 3

This is probably a trivial question but.........I am working on a project that queries a SQL database and if there are no records found for the query, it returns an empty record set & Matlab creates an empty cell array.

Does anyone have a script to loop through the variables in the workspace & clear the ones that meet a certain criteria?

I need to remove variables that meet the criteria below:
1) max value in array is zero
2) all cells are empty

Thanks,
Eric

Subject: Clear Empty Variables in Workspace

From: Ashish Uthama

Date: 23 Mar, 2009 15:48:30

Message: 2 of 3

On Fri, 20 Mar 2009 09:13:01 -0400, Eric
<edette1remove.this1rman@comcast.net> wrote:

> This is probably a trivial question but.........I am working on a
> project that queries a SQL database and if there are no records found
> for the query, it returns an empty record set & Matlab creates an empty
> cell array.

Why not check the return value and clear it if it matches your conditions?

>
> Does anyone have a script to loop through the variables in the workspace
> & clear the ones that meet a certain criteria?
>
> I need to remove variables that meet the criteria below:
> 1) max value in array is zero
> 2) all cells are empty
>
> Thanks,
> Eric

If you _have_ to do this, you might want to look at
WHO - to get a list of your workspace variables
EVAL - to run your conditions on the variable names
CLEAR - ...

EVAL is a 'no-no' in CSSSM, but I cant think of another way.
I would suggest trying something along the lines of my suggestion above.

Subject: Clear Empty Variables in Workspace

From: sesquipedalia

Date: 30 Nov, 2012 23:35:09

Message: 3 of 3

"Eric" wrote in message <gq04ot$it3$1@fred.mathworks.com>...
> This is probably a trivial question but.........I am working on a project that queries a SQL database and if there are no records found for the query, it returns an empty record set & Matlab creates an empty cell array.
>
> Does anyone have a script to loop through the variables in the workspace & clear the ones that meet a certain criteria?
>
> I need to remove variables that meet the criteria below:
> 1) max value in array is zero
> 2) all cells are empty
>
> Thanks,
> Eric

%deletes empty variables
a=who;
 for var=1:length(a)
     b=eval([a{var}]);
     if isempty(b)
         eval(['clear ' a{var} ';'])
     end
 end
 clear a b

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us