all.west
all.west

Reputation: 81

Import coordinates from file .txt with Matlab

I need to plot some trajectories with matlab, i have the coordinates of each points in a file .txt, i work with c++ i want to plot this trajectories with Matlab to make some comparisons, this is an example of the file who contains the coordinates :

515   // this is x
317   // this is y
 0     // i dont want to import this variable
511    // this is x
328     // this is y
20   // i dont want to import this variable
508
353
40
511
 ... etc

there is a function in Matlab that can help me to import only x and y?

file :

 172
 489 
 54460
 283
 469
 54480
 388
 428
 54500
 476
 384
 54520
 555
 350
 54540
 635
 325
 54560
 700
 286
 54580
 760
 250
 54600
 811
 222
 54620
 840
 192
 54640
 856
 171
 54660
 871
 175
 54680
 890
 181
 54700
 930
 170
 54720
 979
 168
 54740

Upvotes: 0

Views: 269

Answers (1)

Suever
Suever

Reputation: 65460

You can read in all values using textscan and simply ignore every third value in the output by using the * in the format specifier.

fid = fopen('filename.txt', 'r');

data = textscan(fid, '%d\n%d\n%*d\n');
[x,y] = data{:};

fclose(fid);

Another option is to read in all data, then reshape and grab the parts you care about.

fid = fopen('filename.txt', 'r');

data = textscan(fid, '%d');
data = reshape(data{1}, 3, []);
x = data(1,:);
y = data(2,:);

fclose(fid);

Upvotes: 3

Related Questions