John
John

Reputation: 4028

What does clear(Filename) do in MATLAB?

files=dir('*.cpp');
for i=1:length(files)
    Filename=files(i).name;
    clear(Filename); 
    ......
end

Could anybody explain what does clear(Filename) do? I think it does not delete the variable Filename because I still see that variable at the workplace.

Upvotes: 4

Views: 276

Answers (2)

abcd
abcd

Reputation: 42225

clear(str) will clear the variable whose name is given by the string in str. From the documentation:

clear('name1','name2','name3',...) is the function form of the syntax. Use this form for variable names and function names stored in strings.

So in your case, it is clearing the variable whose name is the string in files(i).name.

Example:

>> a=1:10;
>> str='a';

%#check what variables are in the workspace
>> whos
  Name      Size            Bytes  Class     Attributes

  a         1x10               80  double              
  str       1x1                 2  char                

>> clear(str)

%#check again
>> whos
  Name      Size            Bytes  Class    Attributes

  str       1x1                 2  char        

Upvotes: 1

Jeff
Jeff

Reputation: 12418

It's clearing the variable files(i).name, where files(i).name is evaluated to the name of the filname

Let's say you had a variable called 'test.cpp', and a filename called 'test.cpp' This would clear the variable 'test.cpp' from your workspace

Upvotes: 1

Related Questions