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!