2013-07-05 13 views
-1

我試圖在MATLAB的imagesc圖上使用ButtonDownFcn。我想通過點擊我在工具欄上創建的自定義按鈕來啓用此功能。Imagesc在MATLAB中使用自定義工具欄動作的ButtonDownFcn問題

ButtonDownFcn將調用方法使其返回使用ButtonDownFcn選擇的像素的位置,以便我可以繪製該像素如何隨時間變化。

注:
- 我使用的指南在MATLAB
- imagesc正密謀3D矩陣。我已經實施了一些代碼,可以讓我通過GUIDE中創建的按鈕瀏覽圖像如何隨時間變化。

我現在正在努力的是imagescButtonDownFcn。我一遍又一遍地閱讀了如何做到這一點(通過在互聯網上的研究),但我似乎無法得到它的工作。

任何幫助表示讚賞。

這裏是我的代碼:

% -------------------------------------------------------------------- 
function ui_throughTime_ClickedCallback(hObject, eventdata, handles) 
% hObject handle to ui_throughTime (see GCBO) 
% eventdata reserved - to be defined in a future version of MATLAB 
% handles structure with handles and user data (see GUIDATA) 
valS = get(handles.txt_zaxis,'string'); 
display(valS); 
val = str2num(valS); 
d = handles.data; 
m = imagesc(d(:,:,val),'parent',handles.axis, 'HitTest', 'off','buttondownfcn',{@getPlace, handles}); 
set(handles.axis,'buttondownfcn',{@getPlace, handles,m}); 

function getPlace(hObject,handles,event_obj,place) 
% data = randn(120,160); 
% ax = axes; 
% imagesc(data,); 
if (gcf == place) 
    pause(1); 
    cursor = get(handles.axis,'CurrentPoint'); % get point 
    % Get X and Y from point last clicked on the axes 
    x = (cursor(1,1)); 
    y = (cursor(1,2)); 
    disp(['x = ' num2str(x) ' y = ' num2str(y)]); 
end 
+0

爲什麼要將圖像的「HitTest」(*選擇性*)設置爲「Off」?試試'imagesc(d(:,:,val),'parent',handles.axis,'ButtonDownFcn',{@ getPlace,handles});'看看它是否有效。 – pm89

+0

因爲我已經通過關閉HitTest來閱讀該文件,所以我將能夠選擇該圖像c在其上形成的軸。我試着把它關掉。 編輯:我刪除了測試,並得到這個錯誤:錯誤使用'matrixexplorer> getPlace(行467) 沒有足夠的輸入參數。 評估圖片時出現錯誤ButtonDownFcn' – EndingWithAli

+0

至少有3種方法可以做到這一點,如下面的答案中所述。最簡單的可能是第一個。如果不清楚,請告訴我。 – pm89

回答

0

下面是一個簡單的例子:

%% Init 
fig_h = figure('CloseRequestFcn', 'run = 0; delete(fig_h);'); 
rgb = imread('peppers.png'); 
run = 1; t = 0; 

%% Loop 
while run 
    t = t + 0.05; 
    imagesc(rgb*sin(t), 'ButtonDownFcn', 'disp(''Clicked'')'); 
    pause(0.01); 
end 

上圖中的'ButtonDownFcn'被使用,因此圖像的'HitTest'屬性必須是'On'

下面是使用軸的'ButtonDownFcn'的情況,由於圖像位於軸的前面,因此圖像的'HitTest'屬性應爲'Off'或軸不可選。

%% Loop 
ax = axes; 
while run 
    t = t + 0.05; 
    imagesc(rgb*sin(t), 'Parent', ax, 'HitTest', 'Off'); 
    set(ax, 'ButtonDownFcn', 'disp(''Clicked'')') 
    pause(0.01); 
end 

它也可以使用數字'ButtonDownFcn'又一次的圖像'HitTest'應該'Off'。然而,在這種情況下,點擊圖像外部(或感興趣的區域)應該以編程方式進行過濾。

希望它有幫助。