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 个评论

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?

请先登录,再进行评论。

 采纳的回答

Fangjun Jiang
Fangjun Jiang 2011-7-19

0 个投票

I have given this advice several times to others. try data=importdata('TempIn.txt') might delight you.

更多回答(2 个)

Walter Roberson
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 ?

1 个评论

everything is working except for the fact that it won't read in more than one line. I have several lines in identical format after it.

请先登录,再进行评论。

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 个评论

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.
within the text file each are on their own lines. When I read it in, it handles the quotations fine but will only read one line in, like the one I put the lines in to show where breaks are. Those are each on their own separate lines in the data.
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!

Translated by