How to read a .csv file with text and numbers? How to create a for loop to do the same function for 1000 times?

3 views (last 30 days)
The problem I have is that I have 1000.csv files. I named from 1.csv to 1000.csv. The first row has some text in columns 2 and 3 and numbers in columns 1,4,5,6,7. These files have 68 columns with headers starting on the second row. (In fact you can take a look at the file here http://rredc.nrel.gov/solar/old_data/nsrdb/1991-2005/tmy3/by_state_and_city.html#N). My first goal is to get the numbers from column number 32, 35, and 38 which I already did. My second goal is to get the headings for the same columns. Third, I need to get the text from column 2 and 3, numbers from column 5 and 6 FROM THE FIRST ROW OF THE FILE. Then, I have to write a new file with all the information that I subtracted from the file. The problems that I have:
1) The problem that I have is that when I tried using textscan to read the first row and the headings, I get this [], and I dont see the text or numbers. Below is what I am using to the read the file:
fid = fopen('3.csv'); %Opens the file TData = textscan(fid, '%d %s %s %f %f %f %d', 'delimiter','\t'); fclose(fid); %Closes the file
City = TData {2}; State = TData {3}; Lat = TData {5}; Lon = TData {6};
2) Can I create a for loop that will do all the process that I described above for all the 1000 files? (Take the data from the original text file and write a new file with this data)
Thank you in advance for your help,
Eric

Accepted Answer

Oleg Komarov
Oleg Komarov on 7 Jul 2011
1) fopen the file
2) Import first line with the following syntax:
C = textscan(fid, 'format', N, 'param', value)
in your case N = 1:
textscan(fid, '%d%s%s%f%f%f%d', 1, 'delimiter','\t')
3) Import the second line with the headers
4) Import the data
5) fclose
You can put all of these operations in a loop.

More Answers (1)

Sean de Wolski
Sean de Wolski on 7 Jul 2011
Have a look at
doc importdata
and
  4 Comments

Sign in to comment.

Categories

Find more on Data Import and Export in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!