Plotting multiple trajectories on map

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 Kommentare

jonas
jonas am 24 Jul. 2018
What are you manually adding? The filenames?
C G
C G am 24 Jul. 2018
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
jonas am 24 Jul. 2018
It was a genuine question with a simple fix :)
C G
C G am 24 Jul. 2018
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.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Pawel Jastrzebski
Pawel Jastrzebski am 24 Jul. 2018

0 Stimmen

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 Kommentare

C G
C G am 24 Jul. 2018
Bearbeitet: C G am 25 Jul. 2018
You are correct that I am only using 2 of the 4 imported columns in this instance. I am using the other columns, time and elevation, in another plot with a similar set up. I figured if it plots on the map here, it will plot the other variables on the other map just as well. No need to bother others with the same problem.
Your for loop to generate tdata did not work. It generated a filled table, but with all of the same information from a previous use of filename. All of my files are text files. I tried combining it with the answer from above, but it generated this error message. All of the filenames have .txt after them and they are in a folder on the path. Not sure what is going on?
Error using importdata (line 137)
Unable to open file.
The plot loop also did not work. I couldn't get past the plotting of the first data set. I keep getting this error message.
figure
plotps(tData.(2),tData(3))
hold on
Argument to dynamic structure reference must evaluate to a valid field name.
tdata is a 121x88 double structure, filled to the brim with information. Not sure what to try next.
Can you explain what this line is doing? I understand the for and the calling up of the data, just not the 6:4 part.
j = 6:4:size
Does there need to be a "." after all tdata?
I'm surprise the import function doesn't work. All I did was taking your the import code you had all along and put it in the loop.
I noticed that my code had few typos:
filename = dir('*.xlsx');
It should have been:
filename = dir('*.txt');
And more importantly:
for j = 6:4:size(tData,2)
plotps(tData.(j),tData(j+1));
end
Had a missing dot in the second element of the plot function, it should have been:
for j = 6:4:size(tData,2)
plotps(tData.(j),tData.(j+1));
end
To explain the plotting loop, the first bit of code was meant to
  • open all of the files in the current folder and import 4 columns
  • concatenate the extracted 4 columns into one file
Now because you always plot column 2 and 3 from your data set, when the data is concatenated, you need to access every 4th which is 2+4, 2+4+4, etc. And this is what this loop does:
for j = 6:4:size(tData,2)
Also could you attach few input text files so we can work on them and see what's wrong with importing?
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
C G am 25 Jul. 2018
Thank you! I am now on my way to creating lots and lots of maps for my thesis.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

C G
C G am 25 Jul. 2018
Bearbeitet: C G am 25 Jul. 2018

0 Stimmen

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

Kategorien

Mehr zu Data Type Identification finden Sie in Hilfe-Center und File Exchange

Gefragt:

C G
am 24 Jul. 2018

Kommentiert:

C G
am 25 Jul. 2018

Community Treasure Hunt

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

Start Hunting!

Translated by