How can I load multiple txt files sequentially?

2 Ansichten (letzte 30 Tage)
Daniel Andruczyk
Daniel Andruczyk am 27 Mär. 2021
Bearbeitet: Jan am 28 Mär. 2021
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 Kommentar
Jan
Jan am 27 Mär. 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.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Jan
Jan am 27 Mär. 2021
Bearbeitet: Jan am 28 Mär. 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 Kommentare
Daniel Andruczyk
Daniel Andruczyk am 27 Mär. 2021
This does not work, I had thought of that, the error message I get when using your suggested code is:
??? Error using ==> textread
File not found.
Error in ==> C:\Work\03. Research\01 HIDRA\HIDRA-Expts\spectroscopy.m
On line 20 ==> [Begin,Spectral] = textread(tite, '%f %f', 'headerlines', 14);
Jan
Jan am 28 Mär. 2021
Bearbeitet: Jan am 28 Mär. 2021
This does not mean, that the is not working, but that there is no file with this name in the specified folder. See [EDITED]

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Language Support finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by