Jonathan
Jonathan

Reputation: 1936

Keeping a GUI open while iterating through images in MATLAB

I want to create a gui that will iterate through images but I'm having trouble keeping the GUI open. It closes after the first image.

display = figure('Name', 'Images');
continue_button = uicontrol('Style', 'pushbutton', 'String', 'Continue',...
        'Callback', %what should I put here%); 

blue_button = uicontrol('Style', 'pushbutton', 'String', 'Blue', 'Position', [400 35 75 20], 'Callback', @show_blue); 

function [] = show_blue(hObject, eventdata, handles)
    imshow(original(:,:,3));
end

for i = 1:length(images)
    imshow(imread(images(i).name));

My code looks like that. I looked at the matlab documentation examples and after callback they had set() but that method doesn't apply to me since I'm changing any component of the gui. Hence, I'm not sure what condition to apply.

I want the gui to display the next image only when I click next.

Upvotes: 1

Views: 38

Answers (1)

Tommaso Belluzzo
Tommaso Belluzzo

Reputation: 23685

Together with your callback handler you should also declare a variable for storing the current image offset being shown. Instead of inserting it into a variable, you can use the UserData property of the button.

display = figure('Name','Images');
continue_button = uicontrol('Style','pushbutton','String','Continue','Callback',@ContinueHandler,'UserData',[1 length(images)]);

% When the figure is created, show the first image by default...
imshow(imread(images(1).name));

Now, when the button is clicked:

function ContinueHandler(obj,evd)
    % retrieve the images data
    img_data = obj.UserData;
    img_off = img_data(1) + 1;
    img_len = img_data(2);

    % Retrieve the next image or restart if the last image is being shown...
    if (img_off > img_len)
        img_nex = 1;
    else
        img_nex = img_off;
    end

    % Clear the axes and show the next image...
    cla();
    imshow(imread(images(img_nex).name));

    % Update the images data...
    obj.UserData = [img_nex img_len];
end

Upvotes: 2

Related Questions