How do I automatically delete a file after the variables needed has been extracted.

조회 수: 6 (최근 30일)
ncvars={'TEC','elevation','time','x_LEO','y_LEO','z_LEO','x_GPS','y_GPS','z_GPS'};
projectdir= "F:\podtc2_data\podTc2_nrt_2022_004";
dinfo=dir(fullfile(projectdir,'*.0001_nc'));
num_files=length(dinfo);
filenames=fullfile(projectdir,{dinfo.name});
TEC_podtc2s=cell(num_files,1);
Elevation_podtc2s=cell(num_files,1);
times=cell(num_files,1);
x_LEOs=cell(num_files,1);
y_LEOs=cell(num_files,1);
z_LEOs=cell(num_files,1);
x_GPSs=cell(num_files,1);
y_GPSs=cell(num_files,1);
z_GPSs=cell(num_files,1);
for i=1 : num_files
this_file=filenames{i};
TEC_podtc2s{i}=ncread(this_file,ncvars{1});
Elevation_podtc2s{i}=ncread(this_file,ncvars{2});
times{i}=ncread(this_file,ncvars{3});
x_LEOs{i}=ncread(this_file,ncvars{4});
y_LEOs{i}=ncread(this_file,ncvars{5});
z_LEOs{i}=ncread(this_file,ncvars{6});
x_GPSs{i}=ncread(this_file,ncvars{7});
y_GPSs{i}=ncread(this_file,ncvars{8});
z_GPSs{i}=ncread(this_file,ncvars{9});
end
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??
Many Thanks
  댓글 수: 2
KSSV
KSSV 2022년 8월 2일
Why you want to delete the files after reading them? What those files has done to you?
Tianchu Lu
Tianchu Lu 2022년 8월 2일
Thank you for your question! The files are useless after the variables that I read is extracted. So it takes up disk space, so I was hoping to write a code where it would automatically delete the file after it has been read. The current need to process around 9000+ nc files

댓글을 달려면 로그인하십시오.

채택된 답변

Steven Lord
Steven Lord 2022년 8월 3일
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:
cd(tempdir)
mkdir example1772805
cd example1772805
!touch myfile1.txt
!touch myfile2.txt
!touch myfile3.jpg
ls
myfile1.txt myfile2.txt myfile3.jpg
Now delete just the two files that match the expression *.txt and confirm that the one .jpg file remains.
delete('*.txt')
ls
myfile3.jpg
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.

추가 답변 (1개)

Rik
Rik 2022년 8월 2일
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?
  댓글 수: 4
Tianchu Lu
Tianchu Lu 2022년 8월 2일
can this function be looped by putting it with the ones that i uploaded earlier? or more how would i loop it so that it wouldnt delete before reading it??
Thanks
Walter Roberson
Walter Roberson 2022년 8월 3일
ncvars={'TEC','elevation','time','x_LEO','y_LEO','z_LEO','x_GPS','y_GPS','z_GPS'};
projectdir= "F:\podtc2_data\podTc2_nrt_2022_004";
dinfo=dir(fullfile(projectdir,'*.0001_nc'));
num_files=length(dinfo);
filenames=fullfile(projectdir,{dinfo.name});
TEC_podtc2s=cell(num_files,1);
Elevation_podtc2s=cell(num_files,1);
times=cell(num_files,1);
x_LEOs=cell(num_files,1);
y_LEOs=cell(num_files,1);
z_LEOs=cell(num_files,1);
x_GPSs=cell(num_files,1);
y_GPSs=cell(num_files,1);
z_GPSs=cell(num_files,1);
for i=1 : num_files
this_file=filenames{i};
TEC_podtc2s{i}=ncread(this_file,ncvars{1});
Elevation_podtc2s{i}=ncread(this_file,ncvars{2});
times{i}=ncread(this_file,ncvars{3});
x_LEOs{i}=ncread(this_file,ncvars{4});
y_LEOs{i}=ncread(this_file,ncvars{5});
z_LEOs{i}=ncread(this_file,ncvars{6});
x_GPSs{i}=ncread(this_file,ncvars{7});
y_GPSs{i}=ncread(this_file,ncvars{8});
z_GPSs{i}=ncread(this_file,ncvars{9});
delete(this_file); %NEW
end

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Large Files and Big Data에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by