Select Numerous Files from Folder Based on Values in Matrix

1 view (last 30 days)
Hi all!
I have one big folder with thousands of files containing data for each day of each year from 1980 to 2000. The name of each .nc file includes the date on which data was recorded and looks something like this ('northward_winds_Np.19910705.nc4', which means this is the data file corresponding to July 5th, 1991). However, I am currently trying to find a more practical way to select only certain files from my big folder using the values in a matrix. That is to say, my matrix has various columns representing the specific month, day, and year on which a certain event happened. As such, I would like to select only the files on which this event happened from the original folder and move the selected files into a smaller folder to further analyze the files as a group. Any suggestions? I greatly appreciate any type of feedback and/or guidance.
Thanks in advance!

Accepted Answer

Sahil Jain
Sahil Jain on 18 Oct 2021
Hi Michelle. Since I do not know the exact structure of your data, I'll provide a few pointers which you can use to solve your problem. I'm assuming that every file is named "northward_winds_Np.YYYYMMDD.nc4" and that the matrix has a separate column indicating whether the event of interest occured on that date or not.
To get the list of files on which the event occured, the following "for" loop will work. The matrix has been named "data" and the first, second and third column are assumed to contain the year, month and day values respectively.
relevant_files = [];
for i=1:length(data)
if % condition to check if event occured
year = num2str(data(i,1));
if numel(num2str(data(i,2)))==1
month = strcat("0",num2str(data(i,2)));
month = num2str(data(i,2));
if numel(num2str(data(i,3)))==1
day = strcat("0",num2str(data(i,3)));
day = num2str(data(i,3));
file_name = strcat("northward_winds_Np.",year,month,day,".nc4");
relevant_files = [relevant_files; file_name];
Once you have the list of all the files you need, you can use the "copyfile" function to copy these files to a different folder.
Sahil Jain
Sahil Jain on 19 Oct 2021
In the code snippet I provided in the answer, could you try adding the following lines of code below it?
for i=1:length(relevant_files)
status = copyfile(relevant_files(i), "myFolder");

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!