Opening files with randomly varying file names
15 次查看(过去 30 天)
显示 更早的评论
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!
0 个评论
采纳的回答
Oleg Komarov
2011-2-22
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 个评论
Walter Roberson
2021-8-22
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')
n = dir(indices)
更多回答(2 个)
Jim Hokanson
2011-7-17
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
2016-11-11
So, How do I loop it over several file names?
1 个评论
Emma Birkett
2017-9-4
编辑:Emma Birkett
2017-9-4
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!
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!