Opening files with randomly varying file names
12 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Jacki
il 22 Feb 2011
Commentato: Candice Cooper
il 22 Ago 2021
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 Commenti
Risposta accettata
Oleg Komarov
il 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 Commenti
Walter Roberson
il 22 Ago 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')
n = dir(indices)
Più risposte (2)
Jim Hokanson
il 17 Lug 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
il 11 Nov 2016
So, How do I loop it over several file names?
1 Commento
Emma Birkett
il 4 Set 2017
Modificato: Emma Birkett
il 4 Set 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!
Vedere anche
Categorie
Scopri di più su Variables in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!