Importing text files below

1 view (last 30 days)
Kin
Kin on 2 Mar 2015
Edited: Stephen23 on 3 Mar 2015
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)

Accepted Answer

Stephen23
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
Kin
Kin on 3 Mar 2015
may I ask what does the {^\n} mean?
Stephen23
Stephen23 on 3 Mar 2015
Edited: Stephen23 on 3 Mar 2015
The textscan documentation explains the format string. Lets have a look:
'%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
  • |

Sign in to comment.

More Answers (0)

Categories

Find more on Data Import and Export in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!