Why is the function "distance" not supported for code generation in MATLAB R2025b?

I’m trying to use the Mapping Toolbox function distance in a MATLAB Function block within a Simulink model that will be compiled into a shared object. I encountered the error:
Function 'distance' not supported for code generation.
Is there another approach or function I can use to achieve the same functionality? 

 Accepted Answer

This distance function from the Mapping Toolbox is not currently supported for code generation in MATLAB R2025b.
To achieve the desired functionality, please consider the following approaches:
1. 
Write Formula Manually Into the MATLAB Function Block:
Below is an example implementation that is supported for code generation. Please test and adapt it to ensure it meets your application’s needs before implementing it:
function [arclen, az] = sphericalDistance(lat1, lon1, lat2, lon2)
%#codegen
% Inputs in degrees, outputs in degrees
% Convert to radians
lat1 = deg2rad(lat1);
lon1 = deg2rad(lon1);
lat2 = deg2rad(lat2);
lon2 = deg2rad(lon2);
% Compute angular distance using spherical law of cosines
delta_lon = lon2 - lon1;
cos_arclen = sin(lat1)*sin(lat2) + cos(lat1)*cos(lat2)*cos(delta_lon);
cos_arclen = min(max(cos_arclen, -1), 1); % Clamp for numerical stability
arclen_rad = acos(cos_arclen);
arclen = rad2deg(arclen_rad);
% Compute azimuth (initial bearing)
y = sin(delta_lon) * cos(lat2);
x = cos(lat1)*sin(lat2) - sin(lat1)*cos(lat2)*cos(delta_lon);
az_rad = atan2(y, x);
az = mod(rad2deg(az_rad), 360); % Normalize to [0, 360)
end
2. Precompute Outputs and Use Lookup Table: If your inputs to the distance function are fixed or change infrequently, you might consider precomputing the outputs and using a lookup table in Simulink.
3. 
Develop C Implementation:
If you have access to a C implementation of the distance function, you could
using "coder.ceval".

More Answers (0)

Categories

Find more on Simulink Coder in Help Center and File Exchange

Products

Release

R2025b

Community Treasure Hunt

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

Start Hunting!