importing tab delimited text file
59 views (last 30 days)
Show older comments
Hi,
I am downloading a text file "A" using textscan. I know that the file is table delimited with unknown number of columns and 34000 rows. Some columns are numbers ,others are strings. I used textscan('A.txt','%s') and what I am getting is 1x1 cell called data . This 1 cell data is 34000x1 where each row contains all the data of the respective rows in the original file (i.e not separated to columns and there isn't any space that separates the rows). Any help about how to download the file is appreciated
1 Comment
Walter Roberson
on 30 Jul 2011
textscan('A.txt','%s') is going to give you the cell array {'A.txt'} -- if the first argument to textscan() is a string, then the string itself is considered to be the input to be scanned.
Answers (5)
Fangjun Jiang
on 28 Jul 2011
try a=importdata('A.txt') to see what you got. Many times, it will give your well-formatted data.
4 Comments
Walter Roberson
on 9 Aug 2017
Note that in more recent versions, importdata by default now returns text as string objects instead of as cell arrays of character vectors. We are seeing people getting caught by that.
Walter Roberson
on 30 Jul 2011
What is the delimiting character? Can any of the strings contain the delimiting character, and if so then how is it indicated that that delimiter is a part of the string rather than marking the end of the string?
Danielle Leblanc
on 30 Jul 2011
1 Comment
Fangjun Jiang
on 30 Jul 2011
It's hard to read your data due to format. Can you paste 3 or 4 lines of your text file here and apply the code format to it?
Walter Roberson
on 30 Jul 2011
You continue to have the same problem that I warned about earlier: textscan() with a string as its first argument reads the string, not a file denoted by the string. The older textread() routine expected a filename as the first argument, but textscan() never does.
fid = fopen('A.txt','rt');
data = textscan(fid, '%f %f %s %s %s %s %s %s %s %f %s %f %s %f %s %f %s %f %s %s %s %d %f %s %f %f %s %f %f %s %f %f %f');
fclose(fid);
The spaces within the quoted string are not important and can be left in or removed as desired.
I coded this in such a way that the dates such as 20090109 are read as numbers, but the time such as 10:21:00 is read as a string. textscan() is not able to directly read formatted times as times.
The output, data, will be a cell array, containing one column vector per column of input, so for example data{2} would be a column vector of floating point numbers corresponding to column 2, one entry per line of input.
3 Comments
Fangjun Jiang
on 30 Jul 2011
You need to check the consistency of your text file. As long as the data format is consistent. The code Walter provided should return correct result. I construct three lines of text using your data. Here is the result:
A.txt
5 6154 T ABN.GG ABN 00077T AA2 N N 4000 A 104.61 + 7.226596 A 20090109 10:21:00 0 @ A Y 7 104.61 + 7.226596 104.61 + 7.226596 104.61 + 7.226596 11636 20090109
5 6154 T ABN.GG ABN 00077T AA2 N N 4000 A 104.61 + 7.226596 A 20090109 10:21:00 0 @ A Y 7 104.61 + 7.226596 104.61 + 7.226596 104.61 + 7.226596 11636 20090109
5 6154 T ABN.GG ABN 00077T AA2 N N 4000 A 104.61 + 7.226596 A 20090109 10:21:00 0 @ A Y 7 104.61 + 7.226596 104.61 + 7.226596 104.61 + 7.226596 11636 20090109
data 1x33 5640 cell
>> data{1}
ans =
5
5
5
>> data{33}
ans =
20090109
20090109
20090109
>> data{18}
ans =
0
0
0
Stephan Koehler
on 7 Sep 2011
I wrote a routine for importing tsv files generated by excel. look at http://www.mathworks.com/matlabcentral/fileexchange/32782
0 Comments
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!