How do I plot on the same figure from 2 functions?

Sorry if this has already been asked and answered, I couldn't find it.
I want to make a 3D plot the orbit of a SC around the Earth and have written a script to do that. I have also written a function to project the Earth but it doesn't map onto the figure correctly. Is there a way to plot onto the same figure from both the script and the function that is called?
Below is the main script:
positionData = data;
figure(1)
% Plot orbit in ECI reference
% Generation of a sphere
hold off
Earth3D(1)
hold on
plot3(positionData(:, 1), positionData(:, 2), positionData(:, 3),'o-r');
% grid; axis equal;
xlabel('X'); ylabel('Y'); zlabel('Z');
title('Orbit ECI (inertial) (m)')
The Earth3D function is intended to project the Earth:
function Earth3D(figure)
% Defines the Earth object and it's parameters as well as the plot size for the transformation.
[x,y,z] = sphere(1000);
xe=x.*6378.137;
ye=y.*6378.137;
ze=z.*6356.752;
[I,~] = imread('STK_map.jpg');
figure(figure)
hold on
warp(xe,ye,-ze,I);
% ax1 = gca; %Creates object for the axis of the plot to allow it to be defined.
% ax1.YDir='normal'; %Defines y axis of the plot with + at the top.
axis equal;
xlabel('x'); ylabel('y'); zlabel('z');
I have also attached the 'data' function which just contains the spacecraft's position data, and the 'STK_map' for the Earth image.
Thank you for any help you can provide.

 採用された回答

Tommy
Tommy 2020 年 5 月 27 日

1 投票

When you name a variable in your function figure, you can no longer access the function figure(). For example:
>> figure = 1;
>> figure(figure)
ans =
1
% no figure is created
That is likely your main problem.
It seems like warp just plots to the current axes. I would recommend working with handles. I think this could work:
positionData = data;
% Plot orbit in ECI reference
% Generation of a sphere
f = figure;
ax = axes(f, 'NextPlot', 'add');
Earth3D(ax)
plot3(ax, positionData(:, 1), positionData(:, 2), positionData(:, 3),'o-r');
% grid(ax, 'on'); axis(ax, 'equal');
xlabel(ax, 'X'); ylabel(ax, 'Y'); zlabel(ax, 'Z');
title(ax, 'Orbit ECI (inertial) (m)')
with your function defined like this:
function Earth3D(ax)
% Defines the Earth object and it's parameters as well as the plot size for the transformation.
[x,y,z] = sphere(1000);
xe=x.*6378.137;
ye=y.*6378.137;
ze=z.*6356.752;
[I,~] = imread('STK_map.jpg');
axes(ax) % make ax the current axes
warp(xe,ye,-ze,I);
% ax.YDir='normal'; %Defines y axis of the plot with + at the top.
axis(ax, 'equal');
%xlabel(ax, 'x'); ylabel(ax, 'y'); zlabel(ax, 'z');

4 件のコメント

Fraser Scanlan
Fraser Scanlan 2020 年 5 月 27 日
It still seems to have the same problem. The Earth is plotted, then the orbit replaces it as opposed to being plotted on the same figure.
Tommy
Tommy 2020 年 5 月 27 日
With 'NextPlot' set to 'on', that shouldn't happen. If you save the axes limits before your call to plot3 and then restore the limits afterwards, you should see that your sphere is still there.
positionData = data;
% Plot orbit in ECI reference
% Generation of a sphere
f = figure;
ax = axes(f, 'NextPlot', 'add');
Earth3D(ax)
lims = axis(ax); % save limits
plot3(ax, positionData(:, 1), positionData(:, 2), positionData(:, 3),'o-r');
axis(ax, lims); % restore limits
% grid(ax, 'on'); axis(ax, 'equal');
xlabel(ax, 'X'); ylabel(ax, 'Y'); zlabel(ax, 'Z');
title(ax, 'Orbit ECI (inertial) (m)')
Are you sure that the data contained within positionData are compatible with the axes limits? It seems that you are plotting the red circles very far away from the globe.
Fraser Scanlan
Fraser Scanlan 2020 年 5 月 27 日
You're right I was using the wrong scale, thank you for helping, it works now!
Tommy
Tommy 2020 年 5 月 27 日
Awesome, happy to help!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeReference Applications についてさらに検索

製品

リリース

R2020a

質問済み:

2020 年 5 月 27 日

コメント済み:

2020 年 5 月 27 日

Community Treasure Hunt

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

Start Hunting!

Translated by