How to read a .csv file with text and numbers? How to create a for loop to do the same function for 1000 times?
조회 수: 5 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
채택된 답변
Oleg Komarov
2011년 7월 7일
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.
추가 답변 (1개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Import and Export에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!