Hi @Olga,
Thank you for posting your question on MATLAB Central — great to see you working on a flight simulator! I'm happy to walk you through how to tackle this.
The good news is that you don't need to manually implement the "check if waypoint is reached, then switch to the next one" logic yourself. MATLAB and Simulink already have the right tools to handle this out of the box. Here's what you need to do:
CONVERT YOUR LAT/LON WAYPOINTS INTO METERS FIRST
Simulink's navigation blocks don't work directly in degrees of latitude and longitude — they work in meters from a local reference point (called a flat-Earth coordinate system). So your first job is to convert your entire waypoint list.
You can do this using the lla2flat MATLAB function inside a MATLAB Function block in Simulink. You set your origin airport as the reference point, and it converts every waypoint into [x, y, z] positions in meters. The Aerospace Blockset also has a dedicated "LLA to Flat Earth" block you can wire directly into your diagram.
lla2flat function: https://www.mathworks.com/help/aerotbx/ug/lla2flat.html
LLA to Flat Earth block: https://www.mathworks.com/help/aeroblks/llatoflatearth.html
USE THE WAYPOINT FOLLOWER BLOCK — IT HANDLES SWITCHING AUTOMATICALLY
This is the key block you've been looking for. You give it your entire list of waypoints (as an N×3 matrix of [x, y, z] positions) all at once, along with your aircraft's current position and heading. It then continuously outputs a "Desired Course" — the heading your plane should be flying right now.
Critically, the block handles waypoint switching automatically. Once your plane gets within a configurable "transition radius" of the current waypoint, the block moves on to the next one on its own. You don't need a loop or any manual counter logic. When the final waypoint is reached, a Status output goes to 1, which you can use to end the simulation or trigger a landing sequence.
Waypoint Follower block documentation: https://www.mathworks.com/help/uav/ref/waypointfollower.html
ADD A HEADING CONTROLLER TO ACTUALLY FLY THE DESIRED COURSE
The Waypoint Follower tells you *where* to point the plane, but you still need a controller to actually steer it there. A simple proportional (P) controller — or a PID if you want more precision — takes the difference between the desired course and the current heading, and outputs a roll command to your aircraft dynamics model.
MathWorks has a worked example that puts all of this together for a fixed-wing aircraft, which I'd strongly recommend as your starting point:
Tuning Waypoint Following Controller for Fixed-Wing UAV: https://www.mathworks.com/help/uav/ug/tuning-waypoint-follower-for-fixed-wing.html
Fourth IF YOU NEED CUSTOM WAYPOINT LOGIC — USE STATEFLOW
If you want more control over what happens at each waypoint (for example, changing altitude, logging arrival times, or handling a missed waypoint), you can add a Stateflow chart on top of the Waypoint Follower. Each state in the chart represents a waypoint, and you write a simple transition condition like "distance to current waypoint < 500 metres" to move to the next state. Stateflow is ideal for this kind of mode-switching logic and keeps your diagram clean and readable.
Stateflow overview: https://www.mathworks.com/help/stateflow
Guidance, Navigation & Control examples: https://www.mathworks.com/help/uav/guidance-navigation-and-control.html
REQUIRED TOOLBOXES
To follow the approach above you will need:
* UAV Toolbox (for the Waypoint Follower block)
* Aerospace Blockset (for LLA/flat-Earth conversion and 6DOF dynamics)
I hope this gets you unblocked! Don't hesitate to follow up if anything is unclear.