2014-01-21 40 views
0

我想在MATLAB中創建一個網格。交互式網格

網格的行數和列數由用戶在運行時輸入。

當用戶點擊在網格上的一個特定塊/平方,

  1. 我需要獲取塊的座標(即(1,1)(2,3)等)

  2. 我還需要爲該方塊/塊着色。

有關我該如何做的任何建議?

回答

1

,可作爲首發:

% draw a rectangle 
% store coordinates in the userdata 
r = rectangle('Position', [1 1 1 1], 'UserData', [1,1], 'FaceColor', 'r'); 
% set the clicked-callback: 
set(r, 'ButtonDownFcn', @showIndex); 

function showIndex(hObject, evt) 
    disp('Clicked on:'); 
    disp(get(hObject, 'UserData')); 
end 

[代碼語法編輯]

編輯:

關於座標: 當然你也可以使用自己的座標,想必你得到一個類似於這樣的循環:

for ix=1:n % loop over columns 
    for iy=1:m % loop over rows 
     % modify coordinates to your needs 
     % e.g. to make the y-index start at 1 from top to bottom: 
     coords = [ix,m-iy+1]; 
     r(ix,iy) = rectangle('Position', [ix,iy,1,1], 'UserData', coords, ...); 
     % remaining stuff... 
    end 
end 
+0

所以你建議我創建用戶輸入的許多矩形?例如,如果用戶輸入他想要一個5x5網格,我應該創建25個矩形? – Dinesh

+0

我試圖創建多個矩形,但座標'(1,1)'從左下方開始,而不是左上方。有關如何處理這種情況的任何建議。 – Dinesh

+0

看到我上面的編輯 - 你可以根據你自己的座標系統自由設置'UserData'。 – sebastian