Plotting multiple trajectories on map
13 views (last 30 days)
Show older comments
I have a rather strange one.
I need to plot multiple trajectories on a map. Right now, I am manually adding each latitude and longitude to the script and plotting away. This is easy enough for a few lines, but not in the long run.
The variables tdump* are specific to a trajectory and are from individually named text files. So... these change depending on the text file. What I have been doing is importing the information I need from each text file and assigning it a numeric variable with the same name.
%Import data from file. Get rid of ugly header.
filename = 'tdump85072712.txt';
delimiterIn = ' ';
headerlinesIn = 12;
C = importdata(filename,delimiterIn,headerlinesIn);
%To assign certain columns to a specific variable
tdump85072712 = C.data(:,9:12);
I then go on to plot each onto a map.
figure
lima ('xy') %Plots a stereographic map
plotps(tdump85072506(:,2),tdump85072506(:,3),'r-')
plotps(tdump85072512(:,2),tdump85072512(:,3),'color', [0 .498 0],'linestyle','-')
plotps(tdump85072518(:,2),tdump85072518(:,3),'k-')
plotps(tdump85072600(:,2),tdump85072600(:,3),'color', [.306 .396 .58],'linestyle','-')
plotps(tdump85072606(:,2),tdump85072606(:,3),'m-')
plotps(tdump85072612(:,2),tdump85072612(:,3),'b-')
plotps(tdump85072618(:,2),tdump85072618(:,3),'y-')
...
Does anyone know of an easier way to achieve the same thing?
4 Comments
Accepted Answer
Pawel Jastrzebski
on 24 Jul 2018
This is roughly how I would go about this problem [code untested]:
% Get all the file names in the folder
filename = dir('*.xlsx');
% Import settings
delimiterIn = ' ';
headerlinesIn = 12;
% empty Table that will hold ALL imported data
tData = []
% Use 'for loop' to import alle the data into one place
for i = 1:numel(filename)
C = importdata(filename,delimiterIn,headerlinesIn);
% add data to a table - 4 columns
tData = [tData C.data(:,9:12)]
end
% plot first set of data
figure
plotps(tData.(2),tData(3))
hold on
% add rest of the plots to the figure
% using 'for' loop
% starting from 2nd set of data
for j = 6:4:size(tData,2)
plotps(tData.(j),tData(j+1));
end
Side note, it's interesting to see that you import 4 columns out each file (columns 9:12) and yet use only column 2:3 from each data set for plotting. Are you using the remaining data for something else? If not just don't import it.
6 Comments
Pawel Jastrzebski
on 25 Jul 2018
OK, I've fixed my original code and this import should work:
% Get all the file names in the folder
filename = dir('*.txt'); % STRUCT
filename = {filename.name}; % CELL
% Import settings
delimiterIn = ' ';
headerlinesIn = 12;
% empty Table that will hold ALL imported data
tData = []
% Use 'for loop' to import alle the data into one place
for i = 1:numel(filename)
C = importdata(filename{i},delimiterIn,headerlinesIn);
% add data to a table - 4 columns
tData = [tData C.data(:,9:12)];
end
clearvars delimiterIn headerlinesIn i C
And that's the output from in mt Matlab:
More Answers (1)
See Also
Categories
Find more on Data Distribution Plots 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!