-1

I'm trying to use the ButtonDownFcn on an imagesc figure in MATLAB. I want to enable this function by clicking on a custom button I have created on the toolbar.

The ButtonDownFcn will call methods that will cause it to return the position of the pixel selected using the ButtonDownFcn, so that I can graph how that pixel changes through time.

NOTES:
- I am using GUIDE in matlab
- imagesc is plotting a 3D matrix. I already have implemented code that allows me to travel through how the image changes through time, using a button created in GUIDE.

What I am struggling with, at the moment, is the ButtonDownFcn of the imagesc. I've read over and over on how to do this (through research on the internet), but I can't seem to get it to work.

Any help is appreciated.

Here is my code:

    % --------------------------------------------------------------------
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
EndingWithAli
  • 449
  • 1
  • 5
  • 11
  • Why have you set the `'HitTest'` (*selectability*) of the image to `'Off'`? Just try `imagesc(d(:,:,val),'parent',handles.axis,'ButtonDownFcn',{@getPlace, handles});` and see if it works. – p8me Jul 05 '13 at 20:17
  • Because I had read that by turning HitTest off I'll be able to select that axis that the imagesc is formed on. Ill try turning it off now. EDIT: I removed hittest and got this error: Error using `matrixexplorer>getPlace (line 467) Not enough input arguments. Error while evaluating image ButtonDownFcn` – EndingWithAli Jul 05 '13 at 20:40
  • There are (at least) 3 ways you can do that as mentioned in the answer below. The easiest may be the first one. Let me know if it's not clear. – p8me Jul 06 '13 at 14:00

1 Answers1

1

Here is a simple example:

%% 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

Above the 'ButtonDownFcn' of the image is being used so the 'HitTest' property of the image must be 'On'.

Below is the case that the 'ButtonDownFcn' of the axes are used and since the image is in front of the axes, 'HitTest' property of the image should be 'Off' or the axes won't be selectable.

%% 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

It's also possible to use the figure 'ButtonDownFcn' and again the image 'HitTest' should be 'Off'. However in this case clicked points of outside of the image ( or interested area) should be filtered programmatically.

Hope it helps.

p8me
  • 1,840
  • 1
  • 15
  • 23