MATLAB: How to plot coastlines on 3D earth

coastlinesearthgeoidMapping Toolbox

I'm trying to plot coastlines on a 3D Earth I made
E=wgs84Ellipsoid
[x,y,z] = ellipsoid(0,0,0,E.SemimajorAxis,E.SemimajorAxis,E.SemiminorAxis);
s = surface(x,y,z);
[N,R] = egm96geoid;
s.FaceColor = 'texturemap';
s.CData = N;
So far this is what I have and i want to overlay coastlines from the mapping toolbox but, I don't know how to do that.

Best Answer

Currently, you are plotting (x, y, z) data as you would on a Cartesian axes. If you want to combine this with (lat, lon) data, you need to somehow convert between the two.
For example, you could use the geodetic2ecef function from Mapping Toolbox to convert from (lat, lon) coordinates to (x, y, z) coordinates:
% Load coastline data

load coastlines
% Convert from (lat, lon) coordinates to (x, y, z) coordinates
[X, Y, Z] = geodetic2ecef(E, coastlat, coastlon, 0); % E is your reference ellipsoid
% Plot (x, y, z) coastline data on top of existing sphere
hold on
plot3(X, Y, Z, "Color", "red", "LineWidth", 5);
hold off
Alternatively, you could consider sticking with (lat, lon) data exclusively and using something like geoglobe:
% Load coastline data
load coastlines
% Initialize geoglobe
f = uifigure();
g = geoglobe(f);
% Plot coastlines on geoglobe
geoplot3(g, coastlat, coastlon, 0, "Color", "red", "LineWidth", 5);
In either case, it's critical to pay attention to the coordinate system that you are working in.