Invalid expression error with function!

7 ビュー (過去 30 日間)
Olivia Colombo
Olivia Colombo 2019 年 2 月 28 日
回答済み: Star Strider 2019 年 2 月 28 日
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

回答 (1 件)

Star Strider
Star Strider 2019 年 2 月 28 日
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.

カテゴリ

Help Center および File ExchangeEvent Functions についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by