MATLAB Answers

C G
0

Plotting multiple trajectories on map

Asked by C G
on 24 Jul 2018
Latest activity Commented on by C G
on 25 Jul 2018
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

Show 1 older comment
Sadly, yes. Please keep the mocking to a minimum. :)
It was a fast way of adding the names without having to code them in. But now, more people want more maps with more trajectories. I am losing my mind very quickly.
jonas 24 Jul 2018
It was a genuine question with a simple fix :)
Oh, I didn't mean to offend. I figured I would get some mocking for being a newbee at matlab coding. I am grateful for any help I can get that will make my life easier when making all of these maps.

Sign in to comment.

2 Answers

回答者: Pawel Jastrzebski 2018 年 7 月 24 日
 採用された回答

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

C G
on 25 Jul 2018
I am not sure why the import step didn't work. I think it might have had something to do with how the files were being imported. I was able to work around this but creating a spreadsheet with a list of file names and then just importing that list as a string. Then generating my data from there. I have and example of the code below, with error messages.
filename = dir('C:\...\1985_10m\tdump*.txt');
% 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
tData =
[]
Error using importdata (line 136)
Unable to open file.
%Change the top 2 lines to:
dir = 'C:\...\1985_10m\'
filename = dir('tdump*.txt');
...
dir =
C:\...\1985_10m\
Index exceeds matrix dimensions.
Thank you for the explanation. I was on that wavelength after playing with the code for a while.
Oddly, I needed to take the . out of the tData.(#) and add a (:,). I am guessing this has to do with how the data is being imported. I have attached 2 example text files, however, these are not in the correct format. These were made to be text files. The original format has an empty extension and were not able to be uploaded. Matlab can import the data from the files simply enough using the code:
delimiterIn = ' ';
headerlinesIn = 12;
% Create an empty Table that will hold ALL imported data
m1Data = [];
% Use 'for loop' to import alle the data into one place
for i = 1:124 %Set this to the number of files you want to import.
C = importdata(tdumpnamesS2(i),delimiterIn,headerlinesIn);
% add data to a table - 4 columns
m1Data = [m1Data C.data(:,9:12)];
end
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:
C G
on 25 Jul 2018
Thank you! I am now on my way to creating lots and lots of maps for my thesis.

Sign in to comment.


回答者: C G
2018 年 7 月 25 日
編集済み: C G
2018 年 7 月 25 日

After a few minutes of tinkering, I got this to work. Please let me know what you think. Still having trouble with the import step, so this was done manually.
%Step 1: Import your file names as a string. This step was done manually.
%Gets rid of the ugly header.
delimiterIn = ' ';
headerlinesIn = 12;
%Create an empty Table that will hold ALL imported data
tData = [];
% Use 'for loop' to import all of the data into one place
for i = 1:100
%Set this to the number of files you want to import. tdumpnamesS2 is my string name
C = importdata(tdumpnamesS2(i),delimiterIn,headerlinesIn);
%add data to a table - 4 columns
tData = [tData C.data(:,9:12)];
end
%plot first set of data
figure
lima('xy') %Plots a stereographic LIMA image of AA, full continent.
plotps(-71.166889,111.366531,'color', [.6 .2 1],'linestyle','none','marker','p','markersize',05, 'markerface',[.6 .2 1])
%The line above marks the end point for all of the trajectories.
plotps(tData(:,2),tData(:,3),'r-'); %Turns the first line red.
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

  0 Comments

Sign in to comment.