Invalid expression error with function!

1 Ansicht (letzte 30 Tage)
Olivia Colombo
Olivia Colombo am 28 Feb. 2019
Beantwortet: Star Strider am 28 Feb. 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

Antworten (1)

Star Strider
Star Strider am 28 Feb. 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.

Kategorien

Mehr zu Earth, Ocean, and Atmospheric Sciences finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by