WindowScrollWheelFcn with slider in Matlab GUI

…衆ロ難τιáo~ 提交于 2019-12-21 21:25:47

问题


I'm making a GUI in Matlab that scrolls through and displays ~600 medical images. I have an axes on which the images are displayed, and a scrollbar that presently goes through images one at a time when the end arrows are pressed.

I'm trying to figure out how to incorporate the WindowScrollWheelFcn so I can use the scroll on the mouse to go through the images faster.

This is my code:

function ct_slider_Callback(hObject, eventdata, handles)

set(gcf, 'WindowScrollWheelFcn', @wheel);
set(gcf, 'CurrentAxes', handles.ct_image_axes);
handles.currentSlice = round(get(handles.ct_slider, 'Value'));
imshow(handles.imageArray(:,:,handles.currentSlice));
text = sprintf('Slice number: %d', handles.currentSlice);
set(handles.ct_slice_number, 'String', text);
guidata(hObject, handles);

function wheel(hObject, callbackdata, handles)
    if callbackdata.VerticalScrollCount > 0
        handles.currentSlice = handles.currentSlice + 1;
    elseif callbackdata.VerticalScrollCount < 0
        handles.currentSlice = handles.currentSlice - 1;
    end
guidata(hObject,handles);

I keep getting the error: "Error using Image_GUI_new>wheel Not enough input arguments."

I don't have extensive experience with GUIs in Matlab so any help would be appreciated.


回答1:


You're pretty close!

By default, callback functions are assigned 2 input arguments when defined as you did:

set(gcf, 'WindowScrollWheelFcn', @wheel);

is equivalent to

set(gcf, 'WindowScrollWheelFcn', @(DummyA,DummyB) wheel);

If you need to add an input argument, the handles structure for instance, you can wrap up all the input variables (i.e. 2 mandatory plus whatever you like) in a cell array as follows:

set(gcf, 'WindowScrollWheelFcn', {@wheel,handles});

Hence the function wheel accepts the 2 mandatory inputs + the handles structure. That should work now.

If I may, using imshow repetitively is not good performance-wise and you might want to use this trick if you are to display many images continuously in your ct_slider_Callback:

1) When displaying the 1st image (here number 1), assign a handle as the output to imshow.

hShow = imshow(handles.imageArray(:,:,1));

2) Then as the callback is executed, instead of calling again imshow, update the cdata property of the hShow handles created:

set(hShow,'cdata',handles.imageArray(:,:, handles.currentSlice)));

You might see that images are displayed more smoothly...

Hope that helps!



来源:https://stackoverflow.com/questions/31416131/windowscrollwheelfcn-with-slider-in-matlab-gui

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!