Function to take grid reference

2 ビュー (過去 30 日間)
keyboard_cuts
keyboard_cuts 2015 年 12 月 9 日
コメント済み: Stephen23 2015 年 12 月 9 日
fairly new to this, would any body be able to tell me how i can create a function that can take a grid reference eg A10, C4, F6, etc and convert it into a row and column index? oh btw the grid has to be ten by ten!

採用された回答

Stephen23
Stephen23 2015 年 12 月 9 日
編集済み: Stephen23 2015 年 12 月 9 日
>> fun = @(s)[upper(s(1))-64,str2num(s(2:end))];
>> fun('A10')
ans =
1 10
>> fun('C4')
ans =
3 4
>> fun('F6')
ans =
6 6
  1 件のコメント
Stephen23
Stephen23 2015 年 12 月 9 日
Try this:
S = input('Enter the grid reference you wish to convert: ','s');
C = upper(S(1))-64
R = str2num(s(2:end))

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

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2015 年 12 月 9 日
Under the condition that it is 10 by 10 exactly,
r1 = GR(1) - 'A' + 1;
if length(GR) == 2
r2 = GR(2) - '0';
else
r2 = 10;
end
You do not specify whether the letters indicate rows or columns so make the appropriate choice [r1,r2] or [r2,r1]
If the grid can be larger than 10 x 10 then the "else" would have to be adjusted. If the grid can be larger than 26 then the r1 logic would need to be adjusted.
  2 件のコメント
Walter Roberson
Walter Roberson 2015 年 12 月 9 日
Note: Stephen's code is more compact; my code is more efficient for the particular requirements that were given.
keyboard_cuts
keyboard_cuts 2015 年 12 月 9 日
Thanks for your response, its much appreciated however like i said im new to this so im still a little unsure. My code now looks like
my_grid= zeros(10,10); %creates the matrix
GR=input('Enter the grid reference you wish to convert')%takes in input
r1 = GR(1) - 'A' + 1 if length(GR) == 2 r2 = GR(2) - '0' else r2 = 10 end however it still does not give me an output answer ie A1 =1,1 or C8= 3,8 or what have you

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

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by