Importing text files below
1 view (last 30 days)
Show older comments
Hi, I have trying to read the text file below where the first col is a string of different length, and it seem to be separated by a tab or something. Can anyone give some advice?
Thx
Symbol Description
1PG.AX 1-page Limited
AADKOA.AX Ardent Leisure Group
AAI.AX Alcoa Inc (USA CUFS)
0 Comments
Accepted Answer
Stephen23
on 2 Mar 2015
Edited: Stephen23
on 2 Mar 2015
textscan is going to be a robust and fast method to read this file:
fid = fopen('temp.txt','rt');
C = textscan(fid,'%s%[^\n]');
fclose(fid);
C{1} contains a cell array of the first column in the text file, C{2} contains all words after that. You could also use this to solve your earlier question in a much neater way:
The test text file is attached:
3 Comments
Stephen23
on 3 Mar 2015
Edited: Stephen23
on 3 Mar 2015
'%s%[^\n]'
The first field is %s, which is defined as Read a string. This matches the first column of data.
The next field is %[^\n], which combines the following two features together:
- %[^...] is explained in the documentation as: Exclude characters inside the brackets, reading until the first matching character
- |
More Answers (0)
See Also
Categories
Find more on Data Import and Export 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!