How Do I get Points in a given Plot?

8 次查看(过去 30 天)
Hello everybody,
I try to create the A star algorithm in MatLab using a Vektorfile with roads to get the best possible route.
I want to create an interactive plot that when the programm runs, you should select any point (with the mouse) on the map and then the route would appear.
Is it even possible?
close all
clear all
clc
format long g
% Straßen Importieren
load('all_geo.mat');
% A und L berechnen
[A,L] = shpToAdjMatrix(all);
Id = L(:,1);
Lat = L(:,2);
Lon = L(:,3);
dist = L(:,4);
% Start und Ende definieren
start = 100;
ende = 2000;
% Current Node
cn = start; % Current Node mit start value initialisieren
% Close und Open initialisieren
closed = false(length(A),1);
open = [];
% Luftlinie h definieren mit der Formel der euklidischen Distanz
h = sqrt((Lat(:) - Lat(ende)).^2+(Lon(:) - Lon(ende)).^2);
% Vektor für Strecke von Ende zum Start definieren
g = zeros(length(Id),1);
% Vektor für Kosten setzen
f = zeros(length(Id),1);
f(:) = f(:) + 2000000;
% Vektor für Parentnode setzen
P = zeros(length(Id),1);
P(cn) = -1000; % -1000 gilt als default Wert
while true
% Wenn Endknoten aus open entnommen wurde dann stop
if (cn == ende)
break;
% Sonst alle Nachbarkanten in Adjazenzmatrix durchlaufen
else
for i = 1 : length(A)
if (A(i,cn) > 0 && closed(i) ~= true)
% Kosten aus Luftlinie und Strecke zum Start berechnen
f_new = h(i) + A(i,cn) + g(cn);
% Wenn kostenNew < konstenAlt dann aktualisieren
if f_new < f(i)
g(i) = g(cn) + A(i,cn);
P(i) = cn;
f(i) = f_new;
temp = [i, f_new];
open = [temp; open];
end
end
end
end
% Knoten in der Close Liste hinzufügen
closed(cn) = true;
% Current Node aus open entfernen
open(open == cn, :) = [];
% minimale Kosten finden und als neue cn definieren
[~,newMin] = min(open(:,2));
cn = open(newMin,1);
end
% A* Route in knoten speichern
N = 1; % counter
knots(1,1) = cn;
PKnot = cn; % parent knode
while(P(PKnot) ~= -1000)
PKnot = P(PKnot);
if P(PKnot) ~= 0
knots(N,1) = PKnot;
knots(N,2) = Lat(PKnot);
knots(N,3) = Lon(PKnot);
end
N = N + 1;
end
% Plots
hold on
grid on
axis equal
% Karte plotten
% boston = plot(img, R);
% Straßen plotten
for i = 1:length(all)
if all(i).CLASS >= 4 % Normale Straßenklassen = 4,5,6,7
local = plot(all(i).X, all(i).Y, 'black', 'LineWidth', 1);
elseif all(i).CLASS < 4 % Autobahnklassen = 1,2,3
highways = plot(all(i).X, all(i).Y, 'red', 'LineWidth', 1.3);
end
end
% Start und Ende markieren
[Lat,Lon] = getpts; % Start
plotST = plot(([Lat,Lon], 'og', 'LineWidth', 1.5, 'MarkerSize', 10));
plotStart = plot(Lat(start), Lon(start), 'og', 'LineWidth', 1.5, 'MarkerSize', 10);
plotEnde = plot(Lat(ende), Lon(ende), 'xg', 'LineWidth', 1.5, 'MarkerSize', 10);
for i = length(knots)-1:-1:-1
% das er auch wirklich die Straße ab geht
if knots(i,1) > length(all)
% Endpunkt abgerufen
tempPosition = knots(i,1)-length(all);
plotRoute = plot(all(tempPosition).X,all(tempPosition).Y, 'g-', 'LineWidth', 2);
else
% Startpunkt abgerufen
plotRoute = plot(all(knots(i,1)).X,all(knots(i,1)).Y, 'g-', 'LineWidth', 2);
end
end
Thanks in advance!

回答(1 个)

Monisha Nalluru
Monisha Nalluru 2021-1-12
From my understanding, you want to user to select points in the plot, based on the selected points route would be ploted on existing figure.
Inorder to perform above operation, there is feature called brush which will help to fetch all the points which user has selected into workspace. In your algoritm while plotting map make
brush('on');
Once your selected the data there are couple of options in toolstrip inorder to export the points into command window, copy to clipboard or create a new variable in workspace
The other option is using ginput
[x,y]=ginput(n) %where n is number of points you want user to select
Hope this is helpful!

类别

Help CenterFile Exchange 中查找有关 Annotations 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by