fail to delete files using the delete command in R2018a
18 views (last 30 days)
Show older comments
Songqiu
on 21 May 2018
Commented: Walter Roberson
on 3 Sep 2021
Hi, everyone,
The current dir is F:\Test
I tested to delete files using the following commands:
TestName = 'F:\Test\Test\Test.xlsx'
delete TestName
But it failed to work:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/190705/image.jpeg)
When I set the current dir to F:\Test\Test and using the following commands:
TestName = 'Test.xlsx'
delete TestName
It also failed to work:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/190706/image.jpeg)
But when I using the delete 'Test.xlsx' command, it succeeded:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/190707/image.jpeg)
Does anyone have the same problem or who can tell me why?
Thank you very much in advance.
1 Comment
Stephen23
on 21 May 2018
"Does anyone have the same problem or who can tell me why?"
Because you are using command syntax, which interprets all trailing inputs as literal strings. So when you enter this:
Delete TestName
it is exactly equivalent to this:
Delete('TestName')
Thus none of your examples (except the last one) are trying to delete that file. If you want to use a variable as an input argument to a function then you will need to use function syntax, as Walter Roberson showed. Note also that none of the examples in the MATLAB help show command syntax being used with an input argument.
Accepted Answer
Walter Roberson
on 21 May 2018
delete(testname)
For an explanation see https://www.mathworks.com/help/matlab/matlab_prog/command-vs-function-syntax.html
2 Comments
Sihem Nour El Houda
on 3 Sep 2021
Hello , sir . I tried to use the delete command many times but it wouldn't work. I can't seem to understand why as I followed many examples. What I'm working on is : if an image verifies a condition that is defined, delete the file !
The code runs but the image file is always present in the folder !
I would really appreciate your help with this as I need it to loop through tens of images prior to the training of a CNN.
Thanks in a advance
More Answers (0)
See Also
Categories
Find more on Resizing and Reshaping Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!