Question about import a .txt file into matlab
1 view (last 30 days)
Show older comments
Johan Sebastian Diaz Tovar
on 10 Oct 2019
Commented: Johan Sebastian Diaz Tovar
on 12 Oct 2019
I'm trying to import a .txt data, but when i used the import data option, something goes wrong and does not appear the file that I want to analyse.
The file is attached. Thank you so much!
3 Comments
Bob Thompson
on 11 Oct 2019
I'm not sure about the exact error you're getting, but I would guess the error line is not reading because you have the row start line. In the textscan documentation the first three inputs are the file ID, the formatspec, and the number of repetitions you want to capture. The format spec you have specified for the error line returns a single cell which contains a series of strings for all individual lines of the file. For me, using the repetition number returned a blank cell with no data. I would suggest getting rid of that number (which I suspect is supposed to be the number of header lines).
Which bits of data are you trying to capture from the file? I see three integers, a comma, and two to three more integers, before a series of spaces with a 0 or -0 as the final characters. Are you looking to capture the numbers on either side of the comma? Or is that supposed to be one number, and the 0 is the second column?
Accepted Answer
Stephen23
on 11 Oct 2019
Edited: Stephen23
on 11 Oct 2019
Your code has several issues, the most significant ones are:
- you incorrectly used the optional third input, apparently to try and define the number of header lines. The correct way to specify the header lines is to use a key-value argument.
- your data uses decimal commas. MATLAB does not handle decimal commas, which means you need to convert the commas to points before converting to numeric: search this forum for different ways to do this.
Here is one simple solution using textscan's very convenient ability to parse character vectors:
fnm = '780 nm animal 1 x=4 y=4_AbsoluteIrradiance_16-18-18-480.txt';
str = fileread(fnm);
str = strrep(str,',','.');
opt = {'HeaderLines',13, 'CollectOutput',true};
fmt = '%f%f';
C = textscan(str,fmt,opt{:});
Giving:
>> out = C{1}
out =
177.84 0
178.06 0
178.28 0
178.5 0
178.72 0
178.93 0
179.15 0
... lots of lines here
348.85 0
349.06 0
349.27 0
349.48 0
349.69 0
349.9 0
350.11 -0.46
350.32 -0.38
350.53 -0.41
350.74 -0.14
350.95 0.03
.... lots more lines here
890.36 -1.06
890.53 -1.71
890.7 -2.42
890.87 -1.44
891.04 -2.31
891.2 -2.19
891.37 -1.34
891.54 -1.7
891.71 -0.93
891.88 0.07
892.05 -0.88
892.22 -0.1
>> size(out)
ans =
3648 2
5 Comments
Stephen23
on 12 Oct 2019
Edited: Stephen23
on 12 Oct 2019
I took a look at your files, and all of them have very mixed up newline characters:
You can see that:
- the first line ends with a linefeed character, but this is immediately followed by a carriage return character. This is extremely odd.
- the rest of the header uses linefeed characters (the Linux standard)...
- but then mysteriously the data uses carriage-return AND line feed characters (the Windows standard).
Very inconstent -> difficult to parse.
The behavior you described is consistent textscan quitting at the first carriage return on the first line of data, after automatically identifying the linefeed as the file's newline character (perhaps using the first line). Are you using Linux by any chance?
In any case, I would suggest two possible solutions to this:
- fix the application that wrote this file, to use linefeed only.
- replace the random newline characters with linefeed only.
The second one you could easily do, just add this line before the textscan call:
str = regexprep(str,'[\r\n]+','\n');
Note that after this you should also adjust the number of header lines to 12.
More Answers (1)
See Also
Categories
Find more on Text Files 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!