Jeff Spector
Jeff Spector

Reputation: 15

continuous slider listener creates a new blank figure

I'm working on a program and have decided to build a GUI around it. What I'd like to start with is pretty simple, load up a movie and be able to scroll through it. I've looked at a lot of the questions regarding listeners, and in fact someone asked this question but the solution there didn't seem to work for me. in my opening function of the gui I have

         handles.output = hObject;
         handles.sliderListener = addlistener(handles.Image_Slider,'ContinuousValueChange', ...
                                              @(hObject, event) Image_Slider_ContValueCallback(...
                                                hObject, eventdata, handles));
         % Update handles structure
         guidata(hObject, handles);

    And then I have the following two call backs :
    function Image_Slider_Callback(hObject, eventdata, handles)
    % hObject    handle to Image_Slider (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    handles=guidata(hObject);
    current_slice = round(get(handles.Image_Slider,'Value'));
    %size(handles.Image_Sequence_Data(:,:,current_slice));


im =imagesc(handles.Image_Sequence_Data(:,:,current_slice),'Parent',handles.Image_Sequence_Plot);
colormap('gray');

which works fine (without the listener everything behaves properly)

AND then I also have

    function Image_Slider_ContValueCallback(hObject, eventdata, handles)
% hObject    handle to Image_Slider (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
handles=guidata(hObject);
current_slice = round(get(handles.Image_Slider,'Value'));
%size(handles.Image_Sequence_Data(:,:,current_slice));
handles=guidata(hObject);

%im =
imagesc(handles.Image_Sequence_Data(:,:,current_slice),'Parent',handles.Image_Sequence_Plot);
colormap('gray');

which I think should be called when the slider is moved continuously. My issue is that EACH time the slider value is changed a blank ("figure 1") appears. The actual GUI data responds correctly, but I don't understand why/where this 'rogue' figure is coming from..

someone please help. Also, any opinions on imshow vs. imagesc as to which is better (this GUI will involve a lot of user interaction with the image)

Upvotes: 1

Views: 281

Answers (1)

Benoit_11
Benoit_11

Reputation: 13945

I had pretty much the same problem once! I don't have access to Matlab right now so I can't test a working example , but for the moment I would suggest to place this line:

handles.sliderListener = addlistener(handles.Image_Slider,'ContinuousValueChange', ...
                                              @(hObject, event) Image_Slider_ContValueCallback(...
                                                hObject, eventdata, handles));

in the CreateFcn of your slider. Maybe a blank figure appears each time you move the slider since it does not know that it's linked to a listener object and thus creates one continuously.

If it does not work, you can call a function outside of the GUI to update the current frame displayed in your axes, for instance:

handles.sliderListener = addlistener(handles.Image_Slider,'ContinuousValueChange', ...
                                              @(a,b) UpdateCurrentFrame);

instead of the callback in your GUI. a and b are dummy input arguments and UpdateCurrentFrame can simply contain a call to imshow for example. It might not be the most elegant way but it worked perfectly for me.

Oh and as for your question about imagesc and imshow, I personnally prefer imshow. In my case, I used text annotations and rectangles for ROI selection and so on, and had little problems with imagesc in terms of images not updating correctly or rectangles being stuck...but it might be that I was not using it correctly though.

Hope that helps!

Upvotes: 1

Related Questions