Extracting data from several txt files

2 views (last 30 days)
MN
MN on 17 Jun 2014
Answered: Joseph Cheng on 17 Jun 2014
I have many text files that I need to extract information from. The first rows (of about 2000) look like this:
Scope Mode Spectrum
Wavelength Intensity
332.50 -0.000
332.88 -0.000
333.26 -6.000
333.64 -5.000
334.03 1.000
334.41 2.000
334.79 -5.000
I want to extract the values of both coloums starting from row 3.
The filenames are:
X4_B1tex.txt, X4_B2tex.txt, X4_B3tex.txt, X4_B4tex.txt
X5_B1tex.txt, X5_B2tex.txt, X5_B3tex.txt, X5_B4tex.txt
and so on. As I have many text-files I'd like some suggestions on how to do this efficiently.
I need:
- One Matrix whose coloums contain the first coloums of the text files (starting from row 3)
- One Matrix whose coloums contain the second coloums of the text files (starting from row 3)

Answers (2)

Sean de Wolski
Sean de Wolski on 17 Jun 2014
Use the Import Tool (right click on one of the files and select "Import Data"). Select the range that you want and then under the green check box select "Generate Function". This will build a function that you can then use to read in all of the files in a for-loop.

Joseph Cheng
Joseph Cheng on 17 Jun 2014
This can be done using a for-loop with dlmread().
%%assumes you have a cell array for the file names;
for i = 1:length(filenames)
temp = dlmread(filenames(1),'\t',2,0) %you'll have to check which delimeter you're using and dlmread starts counting with row0 col0 so thats where the 2 and 0 comes from.
MatOne(:,i) = temp(:,1);
MatTwo(:,i) = temp(:,2);
end

Categories

Find more on Language Fundamentals in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!