user4198354
user4198354

Reputation:

plotting trajectories using matlab

Im trying to plot trajectories on matlab My data file (try.txt) that has the trajectories looks like this:

NumofTrajectories

TrajID1 #ofPoints x y x y....

TrajID2 #ofPoints x y x y....

example:

7

0 23 898.6 673.0 859.1 669.9 813.7 667.8 776.8 664.0 739.8 662.1 699.9 654.7 664.5 649.6 625.3 645.5 588.2 640.6 552.3 634.2 516.6 628.2 477.2 624.3 442.1 613.6 406.7 603.4 369.5 599.8 332.7 594.1 297.4 585.2 258.6 583.7 224.1 573.1 191.2 556.8 152.7 554.0 115.1 546.0 79.6 535.8 

1 8 481.4 624.9 445.9 596.3 374.5 573.9 354.2 541.0 334.2 508.9 327.6 474.1 324.6 437.5 324.2 390.3 

2 24 151.6 570.8 188.3 556.5 225.1 547.7 257.9 529.4 292.9 509.8 326.8 496.8 356.2 476.0 391.2 463.3 423.7 447.9 455.7 431.8 489.2 416.0 524.3 405.3 560.0 395.9 595.8 385.6 632.8 376.1 671.5 372.0 706.9 361.8 742.3 347.3 778.0 334.5 820.5 336.5 856.5 325.0 894.5 309.5 946.1 309.9 990.5 287.0 

3 3 594.2 580.4 566.6 544.3 544.9 509.4 

4 5 281.8 661.9 266.8 623.4 246.2 576.4 229.7 541.0 220.9 498.4 

5 2 563.6 511.3 532.5 479.7 

6 5 571.9 617.7 525.6 576.4 481.0 551.9 456.8 524.2 419.7 474.0 

I'm trying to plot this on matlab my code is as follows:

clc;
clear;
%read the input
importfile('try.txt')

%See how many trajectorys there are convert to number
nTraj=str2num(cell2mat(textdata));

%loop over the trajectories
for i = 1:nTraj
        disp(data(i,1));
        %print the current trajectory number of points
        disp(data(i,2));

        %get the x-y coordinates of each trajectory
        current_traj=data(i,2);
        for j=1:current_traj
            points=data(i,3:j*2+2);    
        end
        %print the x-y coordinates of each trajectory
        %disp(points);

        %seperate the x-y coordinates of each trajectory
        x=points(1:2:length(points)-1)
        y=points(2:2:length(points))

        xlabel('latitude');
        ylabel('longitude');
        plot(x,y,'r');
        grid on ;
        hold on;
end

And function importfile:

function importfile(fileToRead1)
%IMPORTFILE(FILETOREAD1)
%  Imports data from the specified file
%  FILETOREAD1:  file to read

DELIMITER = ' ';
HEADERLINES = 1;

% Import the file
newData1 = importdata(fileToRead1, DELIMITER, HEADERLINES);

% Create new variables in the base workspace from those fields.
vars = fieldnames(newData1);
for i = 1:length(vars)
    assignin('base', vars{i}, newData1.(vars{i}));
end

The code sometimes works and usually gives me an error say:

Index exceeds matrix dimensions.
Error in ==> plotTrajectory at 23
            points=data(i,3:j*2+2);

Can someone explain the error and tell me how to fix it?

Upvotes: 0

Views: 1202

Answers (1)

Steffen
Steffen

Reputation: 2431

The trajectories in try.txt have different lengths. importdata will use the first line to determine the length of your data. If another line is the longest, this line will be split over several lines of your imported matrix. At least this is what debugging shows. I would suggest you use another method to read your file. For me dlmread works:

%read the input
data = dlmread('try.txt', ' ');
% remove header
data = data(2:end,:);
%See how many trajectorys there are convert to number
nTraj=size(data,1);

You can replace your first lines with this code and remove your importfilefunction.

Upvotes: 1

Related Questions