MATLAB Answers

0

How to use shell commands including variables from matlab?

Asked by Pal Szabo on 20 Sep 2017
Latest activity Commented on by Pal Szabo on 20 Sep 2017
I have N files, file1 file2 file3, ... fileN. I know that
!rm file1
removes file1. How can I create a loop, which removes file1 file2 file3 ... fileN? My idea: create array, myfiles = ("myfile1","myfile2","myfile3") Than call:
for i=1:N
!rm myfiles(i)
end
This doesn't work however, matlab tries to remove file "myfiles(i)", not myfile1, myfile2,... myfileN.
How to solve this problem?

  0 Comments

Sign in to comment.

1 Answer

Answer by Walter Roberson
on 20 Sep 2017
 Accepted Answer

for filecell = myfiles
filename = filecell{1};
delete(filename) ;
end
If deletion was just a sample operation
for filecell = myfiles
filename = filecell{1};
cmd = sprintf('rm "%s"', filename );
system(cmd);
end

  1 Comment

Sign in to comment.