Opening files with randomly varying file names

87 views (last 30 days)
Hello,
I am using Matlab to read in csv files generated during data collection. The files all have the format:
PR-2230A_YYYY-MM-DD_00-08-*.csv
where the * is a randomly changing number from 0 to 9. There is no pattern to the number and I have A LOT of data files to open so I need to loop through them. Typically I would declare a filename and use strcat() with variables for any part of the filename that changed, but since I have no idea what the numbers will be I can't do this. The last number is non essential there is only one file per day.
Is there anyway to read in the files without knowing the last number?
i.e. filename = strcat('PR-2230A_',YYYY,'-',MM,'-',DD,'_00-08-',~,'.csv')
I would be greatful for any help!

Accepted Answer

Oleg Komarov
Oleg Komarov on 22 Feb 2011
I propose a different approach:
EDIT: forgot about the wildcard
% Retrieve all the files in a directory
names = dir('C:\Users\Oleg\Desktop\Nuova cartella\PR-2230A_YYYY-MM-DD_00-08-*.csv');
names = {names.name};
Now names will contain only the files which begin with root and you can loop through all of them and load one by one.
Oleg
  7 Comments
Walter Roberson
Walter Roberson on 22 Aug 2021
You accidentally created a variable named dir so dir('NDBC_winds_*.txt') is being treated as an indexing request, equivalent to
dir = randi(9, 1, 200); %for example
indices = double('NDBC_winds_*.txt')
indices = 1×16
78 68 66 67 95 119 105 110 100 115 95 42 46 116 120 116
n = dir(indices)
n = 1×16
1 8 5 2 2 5 6 4 8 6 2 6 9 4 1 4
Candice Cooper
Candice Cooper on 22 Aug 2021
oh my gosh this makes total sense! thanks for helping me debug that!!

Sign in to comment.

More Answers (2)

Jim Hokanson
Jim Hokanson on 17 Jul 2011
Just in anyone else comes across this, use a wildcard instead:
d = dir('PR-2230A_YYYY-MM-DD_00-08-*.csv')
names = {d.name};
The trick is that the dir() function supports wildcards.

BHARGOB DEKA
BHARGOB DEKA on 11 Nov 2016
So, How do I loop it over several file names?
  1 Comment
Emma Birkett
Emma Birkett on 4 Sep 2017
Edited: Emma Birkett on 4 Sep 2017
Here is an example which might help: I have a list of numbers that I need to find in a list of file names. The filenames here are in the format tapsDataP_RandN where P = participant and RandN is a random number. So my list of P numbers is:
actualPNos = [1:4,7:12,14,16:23,25:38,40,43:48,50:52,54:57,59:60];
Get the length of this:
nParts = length(actualPNos);
Set up a matrix where I want to put the data from each of these files (they're quite big data sets) - there will be a new 'sheet' for each dataset:
DataMatrix = nan(175000,15,nParts);
I loop round this loop to get each filename, look up the data in that file and put it in my matrix. Here I have to state the file name, using the number string relating to the current participant number and a wildcard(*) (for the RandN). Then the filename contains a wilcard (csvread doesn't accept this), so use dir to find the entry with that filename and index into that dir struct using .name. This gives a string which I turn into a character vector using char. This can then be used for csvread.
for n = 1:nParts
pNo = actualPNos(1,n);
filename = (['tapsData',num2str(pNo),'_*.mat']);
d = dir(filename);
file = {d.name};
name = char(file);
M = csvread(name);
DataMatrix(:,:,n) = M;
end
Probably not the most elegant, but it works!

Sign in to comment.

Categories

Find more on File Operations in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!