How can I display a vector element with datacursormode by clicking on a point of the plot?

1 vue (au cours des 30 derniers jours)
I have a satellite ground track plot and I'd like to make it possible to click on a point of the plot and display not only the latitude and longitude (its position) but also other information stored in a strings vector. This vector I'm talking about (date) contains several datestr(datetime); I'd like to display the time the satellite passed on that point by clicking on it basically. Here's an example with only two points of what I'm working with:
date = strings(2, 1);
longitude(2, 1);
latitude(2, 1);
d1 = datestr(datetime(2018, 12, 14));
d2 = datestr(datetime(2018, 12, 15));
date(1) = d1; date(2) = d2;
longitude(1) = 40; latitude(1) = 40;
longitude(2) = 50; latitude(2) = 50;
figure('numbertitle', 'off', 'name', 'Ground Track')
plot(40, 40, 'r.', 50, 50, 'b.'); axis([-180 180 -90 90]);
Of course my actual date, longitude and latitude vectors have hundreds of elements. Let's suppose in this example that the satellite passes on (longitude(1), latitude(1)) in d1 and on (longitude(2), latitude(2)) in d2. If the user clicks on the first point, then longitude(1), latitude(1) and date(1) need to be displayed; the same goes for the second point. How does the update function need to be written in order to do this?

Réponse acceptée

Matt J
Matt J le 14 Déc 2018
Modifié(e) : Matt J le 14 Déc 2018
Here's one possibility.
hdt = datacursormode(gcf);
set(hdt,'UpdateFcn', @(s,e) tipcallback(s,e,longitude,latitude,date) );
function output_txt = tipcallback(~,event_obj, long,lat,date)
pos = get(event_obj,'Position');
thedate=date{pos(1)==long & pos(2)==lat};
output_txt{1} = ['Long: ' num2str(pos(1)),' Lat: ', num2str(pos(2))];
output_txt{2}=thedate;
end
untitled.png

Plus de réponses (0)

Produits


Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by