User Input Error? Using input in a function

I am trying to use input to make a functiont that calculates the great circle distance between two sets of coordinates on the surface of the Earth. The problem is I am getting an error on the part that takes input (line 4), long before I get to the calculations. What am I doing wrong?
I've attached the code as well as example coordinates to check it.

7 件のコメント

KSSV
KSSV 2019 年 2 月 27 日
What error you get? What input you tried?
Olivia Colombo
Olivia Colombo 2019 年 2 月 27 日
It's saying that line 4 is an invalid expression.
Olivia Colombo
Olivia Colombo 2019 年 2 月 27 日
I tried the first input listed on the attachment.
Jan
Jan 2019 年 2 月 27 日
編集済み: Jan 2019 年 2 月 27 日
@Olivia: Please post a copy of the original message. "It's saying ..." sounds cute, but is less useful. Which one is the "line 4"? x = input(prompt)?
Older Matlab version do not accept the double quote to define a string. Which Matlab version are you running?
Olivia Colombo
Olivia Colombo 2019 年 2 月 28 日
Error using mydistance (line 4)
Error: Invalid expression. Check for missing multiplication operator,
missing or unbalanced delimiters, or other syntax error. To construct
matrices, use brackets instead of parentheses.
Walter Roberson
Walter Roberson 2019 年 2 月 28 日
exactly what input are you typing in in response to the prompt ?
Olivia Colombo
Olivia Colombo 2019 年 2 月 28 日
I've been testing it with: (with and without various degree symbols)
37N, 76W 37N 9W

サインインしてコメントする。

回答 (1 件)

Stephan
Stephan 2019 年 2 月 28 日

0 投票

Hi,
try:
result = mydistance
function d = mydistance
prompt = 'Input coordinates between which you want to find the great circle distance (XºN, XºW XºN, XºW): \n';
getridof = ["N","W",",",char(176)];
x = input(prompt,'s');
x = double(string(split(replace(x,getridof,""))));
a = acos(sin(deg2rad(x(1)))*sin(deg2rad(x(3)))+cos(deg2rad(x(1)))*cos(deg2rad(x(3)))*cos(abs(deg2rad(x(2)))-deg2rad(x(4))));
d = a*.6371;
disp(['The great circle distance in km is: ',num2str(d)])
end
for input:
Input coordinates between which you want to find the great circle distance (XºN, XºW XºN, XºW):
37°N, 76°W 37°N, 9°W
the result is:
The great circle distance in km is: 0.58165
result =
0.5817
Best regards
Stephan

カテゴリ

ヘルプ センター および File ExchangeGeneral Applications についてさらに検索

質問済み:

2019 年 2 月 27 日

回答済み:

2019 年 2 月 28 日

Community Treasure Hunt

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

Start Hunting!

Translated by