Invalid expression error with function!

7 vues (au cours des 30 derniers jours)
Olivia Colombo
Olivia Colombo le 28 Fév 2019
I am new to using a function, and I'm trying to make a function mydistance that will take user input of coordinates and then calculate the great circle distance on the surface of the earth between the coordinates. The equation is all in there, but I am getting an error on the part that asks for input. Any ideas? Thank you!
(I've been trying it using the input: 37N, 76W 37N, 9W)
function d = mydistance(a)
prompt = 'Input coordinates between which you want to find the great circle distance (XN, XW XN, XW): ';
getridof = ["N","W",","];
x = input(prompt)
x = split(replace(x,getridof," "));
a = acos(sin(x(1))*sin(x(3))+cos(x(1))*cos(x(3))*cos(abs(x(2))-x(4)));
d = a*.6371;
disp(['The great circle distance in km is: ',num2str(d)])
end

Réponses (1)

Star Strider
Star Strider le 28 Fév 2019
Read the coordinates as a string, then do the conversions:
prompt = 'Input coordinates between which you want to find the great circle distance (XN, XW XN, XW): ';
getridof = ["N","W",","];
xc = input(prompt, 's')
xc = split(replace(xc,getridof," "));
x = str2double(xc)
a = acos(sin(x(1))*sin(x(3))+cos(x(1))*cos(x(3))*cos(abs(x(2))-x(4)));
d = a*.6371;
disp(['The great circle distance in km is: ',num2str(d)])
This works, and with your desired inputds, produces:
The great circle distance in km is: 0.93002
when I run it.

Catégories

En savoir plus sur Event Functions dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by