How can I load multiple txt files sequentially?
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
I have a series of text files (with headers) in my directory where they have sequential numbers in the middle of the file, for example
HDR12561__0__15-40-13-410.txt
HDR12561__1__15-40-14-410.txt
HDR12561__2__15-40-15-411.txt
...
...
HDR12561__40_-15-40-53-417.txt
I am able to load in an individual file and extract the data using the following,
tite = sprintf('Experiment_%0.5g/Shot%0.5g/HRD12561__0__15-40-13-410.txt',shot,shot);
[Begin,Spectral] = textread(tite, '%f %f', 'headerlines', 14);
wavelength = Begin;
intensity = (Spectral-65);
but I want to be able to go through all the files in the sequence from 0 - 40 in say a for loop without needing to write out individual files each time. I was hoping I could put in something like HDR*__num__*.txt where num is the variable I am sequencing through to pull the files.
I am sure there is an easy way to do this, but everything I have tried is not working. The code I was working on and thought would work is
tite0 = sprintf('Experiment_%0.5g/Shot%0.5g/*.txt',shot,shot);
files = dir(tite0);
n = numel(files)
for i = 1:n;
j = i-1;
tite = sprintf('Experiment_%0.5g/Shot%0.5g/HRD12561__%0.5g__*.txt',shot,shot,j);
[Begin,Spectral] = textread(tite, '%f %f', 'headerlines', 14);
size(Begin), size(Spectral)
wavelength = Begin;
intensity(:,i) = (Spectral-65);
end;
Any help is appreciated
1 Commento
Jan
il 27 Mar 2021
We cannot run your code due to the missing input data. You mention, that it is not working. Then please share the information about the occurring error with the readers. It is easier to solve a problem than to guess, what the problem is.
Risposte (1)
Jan
il 27 Mar 2021
Modificato: Jan
il 28 Mar 2021
A bold guess: %0.5g is not the matching format. Try this:
tite = sprintf('Experiment_%0.5g/Shot%0.5g/HRD12561__%d__*.txt', shot, shot, j);
% ^^
[EDITED] Oh, I've overseen it:
tite = sprintf('Experiment_%0.5g/Shot%0.5g/HRD12561__%d__*.txt', shot, shot, j);
% ^ ???
There is a star in the name of the file. Of course Matlab cannot guess how to replace this.
The pattern of the file names is tricky:
HDR12561__0__15-40-13-410.txt
HDR12561__1__15-40-14-410.txt
HDR12561__2__15-40-15-411.txt
...
What is the relation between the two different changing indices?
It looks like you have encoded important information in the name of the file, not in the contents. This makes accessing this information more complicated. An automatic creation of the file names is not possible, if the pattern is encoded in the names itself. Then you jhave to import the list of names at first:
BaseFolder = '/Your/Base/Folder';
Experiment = sprintf('Experiment_%0.5g/Shot%0.5g/', shot, shot);
FileList = dir(fullfile(BaseFolder, Experiment, '*.txt'));
for iFile = 1:numel(FileList)
File = fullfile(FileList(iFile).folder, FileList(iFile).name);
[Begin,Spectral] = textread(File, '%f %f', 'headerlines', 14);
end
2 Commenti
Vedere anche
Categorie
Scopri di più su Language Support in Help Center e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!