Igl3
Igl3

Reputation: 5108

Remove a variable from Workspace by String name

Let's say I got a variable var1 in my Workspace and a struct containing the name of this variable as field e.g:

a = 
    Name: 'var1'
      a1: 1

I want to remove the variable var1 from my workspace by using the field from the struct.

I know that I can clear var1 by clear var1 or clear 'var1'

As clear a.Name does not work, I played around with using the eval function, but I can't get it to work.

Any Idea is appreciated!

Upvotes: 0

Views: 386

Answers (1)

TroyHaskin
TroyHaskin

Reputation: 8401

Use the function form to call clear as command form "always passes inputs as literal text and cannot pass variable values".

>> var1 = 2;
>> a.Name = 'var1';
>> whos()
  Name      Size            Bytes  Class     Attributes

  a         1x1               184  struct              
  var1      1x1                 8  double              

>> clear(a.Name);
>> whos()
  Name      Size            Bytes  Class     Attributes

  a         1x1               184  struct  

Upvotes: 2

Related Questions