# Index in position 2 exceeds array bounds (must not exceed 1) ??

3,880 ビュー (過去 30 日間)
Kaleesh Bala 2018 年 7 月 3 日

Why does the following function produce the error "Index in position 2 exceeds array bounds (must not exceed 1)"?
function test2
close all
prompt ={'Enter the x1 value: ','Enter the y1 value: ','Enter the x2 value: ','Enter the y2 value: '};
Is = inputdlg(prompt);
Is = str2double(Is);
%Eucleidan distance
D = sqrt((Is(1,2)-Is(1,1))^2 + (Is(1,4)-Is(1,3))^2);
fprintf ('the distance value obtained is %d',D)
end

#### 0 件のコメント

サインイン to comment.

### 採用された回答

Guillaume 2018 年 7 月 3 日

The error occurs because the vector “Is” is a 4-by-1 column vector (not a 1-by-4 row vector) so that the valid element positions are (1,1), (2,1), (3,1), and (4,1). If you try to access any index value beyond 1 in the 2nd position, you will encounter the error.

#### 1 件のコメント

Kaleesh Bala 2018 年 7 月 3 日
Yup! sorry, it was assumed to be a row vector. Now it works fine!
function test2
close all
prompt ={'Enter the x1 value: ','Enter the y1 value: ','Enter the x2 value: ','Enter the y2 value: '};
Is = inputdlg(prompt);
Is = str2double(Is);
fprintf('the value of Is %d',Is)
%Eucleidan distance
D = hypot(Is(3)-Is(1), Is(4)-Is(2));
fprintf ('the distance value obtained Dis %d',D)
end

サインイン to comment.

### その他の回答 (0 件)

サインイン してこの質問に回答します。

R2018a