Loading delimited text file

I'm having trouble loading the following text file. That is delimited as such: --------------
116,1.7874926329,-426.0292053223,45.1162757874; 13P 226
116,2.7424895763,-426.0281066895,45.0740852356; 15P 227
116,3.7051227093,-426.0320434570,45.0093040466; 17P 228
-------------- (ignore spaces between lines, that's just for the forum)
I want all the rows but only columns 1-3.
I tried dlmread('data.txt', ',', 0, 1) but that gives me a bunch of 0 rows and doesn't capture all the rows.
Any suggestions?

Answers (1)

use importdata(). Ignore the empty lines between data lines. Also ignore your dash lines.
>> a=importdata('test.txt')
a =
116 1.7875 -426.03 45.116 13 226
116 2.7425 -426.03 45.074 15 227
116 3.7051 -426.03 45.009 17 228
>> b=a(:,1:3)
b =
116 1.7875 -426.03
116 2.7425 -426.03
116 3.7051 -426.03

4 Comments

Using 'importdata' the result is a single-column cell.
Here is some sample data:
116,1.7874926329,-426.0292053223,45.1162757874; 13P 226
116,2.7424895763,-426.0281066895,45.0740852356; 15P 227
116,3.7051227093,-426.0320434570,45.0093040466; 17P 228
116,4.6660690308,-426.0295410156,44.9241867065; 19P 229
116,5.6226024628,-426.0305480957,44.8177299500; 21P 230
All right, importdata() differs at different MATLAB version.
fid=fopen('test.txt','rt');
a=textscan(fid,'%f,%f,%f%*[^\n]','collectoutput',1);
fclose(fid);
a =
[5x3 double]
>> a{1}
ans =
116.0000 1.7875 -426.0292
116.0000 2.7425 -426.0281
116.0000 3.7051 -426.0320
116.0000 4.6661 -426.0295
116.0000 5.6226 -426.0305
I receive an error due to 'CollectOutput':
a=textscan(fid,'%f,%f,%f%*[^\n]','CollectOutput',1);
??? Error using ==> textscan
Unknown option 'CollectOutput'.
Then don't use it. a=textscan(fid,'%f,%f,%f%*[^\n]') works too. The result is in a{1},a{2},a{3}.

Sign in to comment.

Categories

Tags

Asked:

lex
on 30 Aug 2011

Community Treasure Hunt

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

Start Hunting!