Want to use a guide pushbutton to delete all files except some in folder which gui is residing.

7 views (last 30 days)
I have a code that uses Matlabs GUIDE and throughout the process many .m, .mat, .png content is created and stored in the folder where the GUI resides. I have created a pushbutton that when pressed restarts the process but I also would like it to delete All but specific specified files that have names that do not change. For example the project Folder contains: Project1.m, project1.fig, HelpMakeProjectWork.m, pic.png, Creation.m, Creation2.mat.... Then delete all besides the first three listed needed files.
Thank you so much

Accepted Answer

Geoff Hayes
Geoff Hayes on 10 Jan 2017
Tessa - rather than mixing files that persist with those that are temporary, why not create a TEMP directory and save all of those temporary files to it? That way, when you push the button on your GUI, you could just remove that TEMP directory and all of its files. The TEMP directory can be a sub-directory/folder from your parent one (that where you have your Project1.m, Project1.fig, and HelpMakeProjectWork.m files) and you could delete it as
function pushbutton1_Callback(hObject, eventdata, handles)
if isfield(handles, 'tempDirectory')
rmdir(handles.tempDirectory,'s');
end
The above assumes that you have updated the handles structure with a path to the temporary directory. You can do this from within the OpeningFcn of your GUI as
function myGui_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
tempDir = fullfile(pwd,'TEMP');
status = mkdir(tempDir);
if status
handles.tempDirectory = tempDir;
end
guidata(hObject,handles);
See rmdir and mkdir for details.
  3 Comments
Geoff Hayes
Geoff Hayes on 10 Jan 2017
Tessa - then I suspect that you will need to use dir to get a list of the files and folders in the current directory. Then you will want to iterate through this list and delete those elements that are files and do not match any of the three that you wish to keep.

Sign in to comment.

More Answers (0)

Categories

Find more on Migrate GUIDE Apps 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!