How can I add the "time" info in a datatip in order to know when my object is in a specific [X,Y,Z] position along his trajectory, which is a plot of an ode45 solution?

6 views (last 30 days)
Good evening, everyone!
I'm actually working on an aerobrake of an orbiter on Mars, and to make it work out, I first have to... reach Mars, of course. I'm doing an integration over a whole martian year, with 24 hours steps. I'd like to be able to specify the number of the day (0 to 687) corresponding to a clicked position of my probe, on the plotted trajectories graph (in which we can see Earth and Mars' orbits, as well as the probe trajectories depending on different initial conditions in a loop, to determine the best initial conditions to join Mars).
I'm a neophyte in Matlab, trying to learn what I need to do specific things, thus I have no idea on how are working Callback functions. I tried to edit the Callback to make appear the "t" used by ode45 (which is a 687x1 vector), but it always resulted in Matlab saying that he's unable to update the datatip using my new function. I guess it's a trivial question, not even requiring my code to find a solution, but in case you need it, here is part of my script, my odefun: (hope it'll be enough)
function dydt = odefun(t,y)
Gcav = 6.67408e-11;
Ms = 1.9891e+30; %Soleil
Mt = 5.972e+24; %Terre
Mm = 6.4191e+23; %Mars
dydt = zeros(18,1);
dydt(1) = -Gcav*Ms*y(4)/((sqrt(y(4)^2 + y(5)^2 + y(6)^2))^3);
dydt(2) = -Gcav*Ms*y(5)/((sqrt(y(4)^2 + y(5)^2 + y(6)^2))^3);
dydt(3) = -Gcav*Ms*y(6)/((sqrt(y(4)^2 + y(5)^2 + y(6)^2))^3);
dydt(4) = y(1);
dydt(5) = y(2);
dydt(6) = y(3);
dydt(7) = -Gcav*Ms*y(10)/((sqrt(y(10)^2 + y(11)^2 + y(12)^2))^3);
dydt(8) = -Gcav*Ms*y(11)/((sqrt(y(10)^2 + y(11)^2 + y(12)^2))^3);
dydt(9) = -Gcav*Ms*y(12)/((sqrt(y(10)^2 + y(11)^2 + y(12)^2))^3);
dydt(10) = y(7);
dydt(11) = y(8);
dydt(12) = y(9);
dydt(13) = (-Gcav*Ms*y(16)/((sqrt(y(16)^2 + y(17)^2 + y(18)^2))^3)) + (-Gcav*Mt*(y(16)-y(4))/((sqrt((y(16)-y(4))^2 + (y(17)-y(5))^2 + (y(18)-y(6))^2))^3)) + (-Gcav*Mm*(y(16)-y(10))/((sqrt((y(16)-y(10))^2 + (y(17)-y(11))^2 + (y(18)-y(12))^2))^3));
dydt(14) = (-Gcav*Ms*y(17)/((sqrt(y(16)^2 + y(17)^2 + y(18)^2))^3)) + (-Gcav*Mt*(y(17)-y(5))/((sqrt((y(16)-y(4))^2 + (y(17)-y(5))^2 + (y(18)-y(6))^2))^3)) + (-Gcav*Mm*(y(17)-y(11))/((sqrt((y(16)-y(10))^2 + (y(17)-y(11))^2 + (y(18)-y(12))^2))^3));
dydt(15) = (-Gcav*Ms*y(18)/((sqrt(y(16)^2 + y(17)^2 + y(18)^2))^3)) + (-Gcav*Mt*(y(18)-y(6))/((sqrt((y(16)-y(4))^2 + (y(17)-y(5))^2 + (y(18)-y(6))^2))^3)) + (-Gcav*Mm*(y(18)-y(12))/((sqrt((y(16)-y(10))^2 + (y(17)-y(11))^2 + (y(18)-y(12))^2))^3));
dydt(16) = y(13);
dydt(17) = y(14);
dydt(18) = y(15);
end
Thanks a lot in advance for your help, and my apologizes if there's already an answer for this. I searched, but haven't found anything that would fit my problem.
EDIT: Okay, I've found a way to show something else than the position of my clicked object. Unfortunately, it only shows the local time where I click on the selected position, I've got no use of that... But we're slowly progressing, I feel like I just have to adapt that, even though I actually have no clue yet on how to do so. Here is the callback, for info:
function output_txt = NewCallback(obj,event_obj)
% Display the position of the data cursor
% obj Currently not used (empty)
% event_obj Handle to event object
% output_txt Data cursor text string (string or cell array of strings).
pos = get(event_obj,'Position');
time = datestr(now,0);
output_txt = {['T: ',time],['X: ',num2str(pos(1),4)],...
['Y: ',num2str(pos(2),4)]};
% If there is a Z-coordinate in the position, display it as well
if length(pos) > 2
output_txt{end+1} = ['Z: ',num2str(pos(3),4)];
end
EDIT2: Okay, I've found a way to get the number of the day corresponding to the position given by my click on the ode45 plot. I've assigned the temporary positions (pos(1:3)) to my base workspace with :
pos = get(event_obj,'Position');
assignin('base','memb1',pos(1));
assignin('base','memb2',pos(2));
assignin('base','memb3',pos(3));
so that I can use it in my base workspace. By then I've been able to get the desired data with:
find(ismember(y(:,16:18),[memb1 memb2 memb3],'rows')==1)
My issue is now that the callback doesn't seem to be able to calculate it, while in my base workspace, there's no issue. But it isn't visually pleasing, I'd like it to be shown on the data window.
I suppose that the issue here is the y. Anytime I try to calculate anything in the callback using y, nothing happens. I guess the workspace is now the problem. Even if I try to make y a global matrix, it doesn't work. Maybe the callback function thinks the matrix is called before being defined? But if it were the case, wouldn't
evalin('base',[string(find(ismember(y(:,16:18),[memb1 memb2 memb3],'rows')==1))])
do the trick? Seems like there's another issue then. To be continued...
EDIT3: Solution found. Not really elegant, but it works quite good, so I'll post it here, maybe it could help someone else having issues with workspaces and Callback functions!
function output_txt = SondeCallback(obj,event_obj)
% Display the position of the data cursor
% obj Currently not used (empty)
% event_obj Handle to event object
% output_txt Data cursor text string (string or cell array of strings).
pos = get(event_obj,'Position');
assignin('base','memb1',pos(1));
assignin('base','memb2',pos(2));
assignin('base','memb3',pos(3));
JourSonde = evalin('base','find(ismember(y(:,16:18),[memb1 memb2 memb3],''rows'')==1)');
assignin('base','JourSonde',JourSonde);
output_txt = {['T: ', num2str(JourSonde)],['Xsonde: ',num2str(pos(1),4)],...
['Ysonde: ',num2str(pos(2),4)],['Zsonde: ',num2str(pos(3),4)],...
['Xmars: ',num2str(evalin('base','y(JourSonde,10)'),4)],...
['Ymars: ',num2str(evalin('base','y(JourSonde,11)'),4)],...
['Zmars: ',num2str(evalin('base','y(JourSonde,12)'),4)],...
['Xterre: ',num2str(evalin('base','y(JourSonde,4)'),4)],...
['Yterre: ',num2str(evalin('base','y(JourSonde,5)'),4)],...
['Zterre: ',num2str(evalin('base','y(JourSonde,6)'),4)]};

Answers (0)

Categories

Find more on Interactive Control and Callbacks 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!