How can I load multiple txt files sequentially?

조회 수: 2 (최근 30일)
Daniel Andruczyk
Daniel Andruczyk 2021년 3월 27일
편집: Jan 2021년 3월 28일
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
Jan
Jan 2021년 3월 27일
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.

댓글을 달려면 로그인하십시오.

답변 (1개)

Jan
Jan 2021년 3월 27일
편집: Jan 2021년 3월 28일
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
Daniel Andruczyk
Daniel Andruczyk 2021년 3월 27일
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 2021년 3월 28일
편집: Jan 2021년 3월 28일
This does not mean, that the is not working, but that there is no file with this name in the specified folder. See [EDITED]

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Language Support에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by