Reading Input Files problem
이전 댓글 표시
I've been trying to write a code that will read in a text file with a fixed number of columns, but will need have different numbers of rows depending on the file. The code will read in the first row of data, but I cannot get it to read in the other rows. Any thoughts on how I can get this to work?
fileIn = 'TempIn.txt'; fileOut = 'TempOut.txt';
fid1 = fopen(fileIn); C = textscan(fid1,'%s %d %s %s %c %s %s %s %n %s %s %s %s %s'); fclose(fid1);
1667-68 856 102351 SUSC F ASY 04/09/11 12:00 860 "38.69545,76.26101666666666" 04/11/11 3:00 "38.72035,76.276" "Capture Broad Creek, Release Broad Creek - flew out 100 feet, bathed then flew off to capture area" 1667-68 857 102360 SUSC M ASY 04/09/11 12:00 1200 "38.69545,76.26101666666666" 04/11/11 3:00 "38.72035,76.276" "Capture Broad Creek, Release Broad Creek - flew out 100 feet, bathed then flew off to capture area" 1667-68 858 102363 SUSC M ASY 04/09/11 12:00 1120 "38.69545,76.26101666666666" 04/11/11 3:00 "38.72035,76.276" "Capture Broad Creek, Release Broad Creek - flew out 100 feet, bathed then flew off to capture area" 1667-68 859 102362 SUSC M ASY 04/09/11 12:00 1100 "38.69545,76.26101666666666" 04/11/11 3:00 "38.72035,76.276" "Capture Broad Creek, Release Broad Creek - flew out 100 feet, bathed then flew off to capture area" 1667-68 860 102361 SUSC M ASY 04/09/11 12:00 1100 "38.69545,76.26101666666666" 04/11/11 3:00 "38.72035,76.276" "Capture Broad Creek, Release Broad Creek - flew out 100 feet, bathed then flew off to capture area"
1667-68 | 856 | 102351 | SUSC | F | ASY | 04/09/11 | 12:00 | 860 | "38.69545,76.26101666666666" | 04/11/11 | 3:00 | "38.72035,76.276" | "Capture Broad Creek, Release Broad Creek - flew out 100 feet, bathed then flew off to capture area"
댓글 수: 1
Chirag Gupta
2011년 7월 19일
This should typically read the whole file! Are you sure the format specifier are correct? Can you paste a couple of lines of sample data from your file?
채택된 답변
추가 답변 (2개)
Walter Roberson
2011년 7월 19일
0 개 추천
If you only want to read one line, you should have ,1 after the format string.
It is usually not a good idea to use a space before %c, as it becomes ambiguous (to the programmer if no-one else) as to whether you want to skip whitespace before the single character read or whether the single character read should read even a whitespace character if that is what happens to be there.
Is there a reason you are using %n instead of %f64 ? For example is it possible you meant \n instead of %n ?
Walter Roberson
2011년 7월 19일
Matlab's %s format will not read in an entire "" delimited string. To read in a "" delimited string, you need
..."%[^"]"...
The string that is read in in response will have the "" stripped off.
The %s format also will not read in strings that have spaces in them -- not unless you fiddle around with the Whitespace parameter.
댓글 수: 3
Walter Roberson
2011년 7월 19일
The delimited example helps.
The third ('102351') field would seem more compatible with a numeric format specifier, but %s is plausible.
Trying to use %s to read the "" string that contains spaces is your major problem.
You spoke of reading lines, but according to the data you posted, everything is one long line with 14 fields per group and the 1667-68 after the first are in the middle of the line. That will not in itself cause a problem for the format you are using, but if you _did_ have a real line break then reading the "" enclosed string would be easier.
Joe Reynolds
2011년 7월 19일
Walter Roberson
2011년 7월 19일
I am _certain_ that textscan() will not parse "" delimited strings with embedded spaces as being a single string for %s purposes, not unless you have a Whitespace or Delimiter option.
Replace the final %s in the format with "%[^"]" (the " are literal there, include them in the format) OR replace the final %s in the format with %[^\n]" . The form that uses %[^"] is usable in the middle of a line, whereas the %[^\n] is suitable only for reading to the end of the line.
카테고리
도움말 센터 및 File Exchange에서 Large Files and Big Data에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!