How to delete a .csv in my directory

28 views (last 30 days)
I have a csv that is a subset of the original data (part of my script is to create it), and I am looking for a way to delete the file at the begining of my script each time since a new one is always created. Is there a way to do such a thing?
It isn't in my main directory, but is in a subfolder from it.

Accepted Answer

Guillaume on 19 Feb 2020
delete subfolder\filename.csv %relative path to current directory
But you shouldn't rely on the main directory and instead use full paths everywhere in your script. This way the data can be located anywhere independently on where the code is.
datafolder = 'C:\somewhere\somefolder'
%importing some data with whichever function you normally use. Using readmatrix here for demo:
filename = 'somefile.csv';
data = readmatrix(fullfile(datafolder, filename));
%deleting a particular file in the data folder:
delete(fullfile(datafolder, 'somefile.txt'));
%saving an image
imwrite(img, fullfile(datafolder, 'fancyimage.png'))

More Answers (0)


Find more on File Operations 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!