How do I automatically delete a file after the variables needed has been extracted.
5 views (last 30 days)
for i=1 : num_files
So, above is my code for automatic extractation of the variables needed for my project. So the problem I am having is how would I optimise rge following code to delete the files after the variables has been extracted. From the mathworks website, it recommended a function of 'delete' is there is a better way of optimsing the loop where once it reads a singles nc file it would delete that specific file rather than using 'delete' to delete the whole folder at the end??
Steven Lord on 3 Aug 2022
You could delete the files one at a time, or if you are careful you could delete them with one delete call at the end of your code. Let's make some sample files:
Now delete just the two files that match the expression *.txt and confirm that the one .jpg file remains.
But I kind of agree with the other posters about whether deleting your data files is a good idea. If you do that and something goes wrong, or you decide you need additional information from the file that you did not retrieve when you processed it the first time, or you simply want to reproduce the results of your analysis (which is generally thought to be a good thing), you may not have the original data to do so.
More Answers (1)
Rik on 2 Aug 2022
You can use delete directly on a specific file.
But the question still stands. Generally you want to process data from one form to another. I personally don't delete files automatically, unless the same code writes the analysis results somewhere. Are you sure you don't want to check whether the analysis and writing of the result is completed successfully before you delete the source files?