Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
How to close a figure without critical error

Subject: How to close a figure without critical error

From: Austin

Date: 3 Aug, 2010 14:24:06

Message: 1 of 4

Hi,

I wrote a program for viewing 4D images (x,y,z,t), one at a time. The user uses the left and right arrows to scroll through the z dimension, and the up and down arrows to scroll through the t dimension.

The problem is, I cannot figure out how to make it so that given a certain keystroke (for example, pressing enter), the figure will close and the function will terminate. I tried to put a bit of code like "if keystroke == enter, close", but that makes matlab crash.
Any ideas?


% Austin Bishop. April 2010. apb14@case.edu
%
% function scroll(vol)
% parameters:
% 'vol' is a 3D image matrix (x,y,z) where z is the image number.
% This function allows you to use the left and right arrow keys
% to browse through a collection of z images.
% Press leftarrow to move back
% Press rightarrow to move forward
% CTRL-C to QUIT
% Uses imshow, so doubles should be converted to uint8
%
function scroll(vol)

figure('KeyReleaseFcn',@cb)
i=1;
k=1;
flag=0;
imshow(imresize(vol(:,:,i,k),2),[]);
maxi=size(vol,3);
maxk=size(vol,4);
title(strcat('Slice: ',num2str(i),'(',num2str(maxi),') Timepoint: ',num2str(k),'(',num2str(maxk),')'));


%fourflag=maxk>1;
    function cb(src,evnt)
        
        key = evnt.Key;
        if strcmp(key,'leftarrow')==1
            i=i+-1;
        end
        if strcmp(key,'rightarrow')==1
            i=i+1;
        end
        if strcmp(key,'uparrow')==1
            %if fourflag==1
            k=k+1;
            %end
        end
        if strcmp(key,'downarrow')==1
            %if fourflag==1
            k=k-1;
            %end
        end
        
        if i<=0
            i=maxi;
        end
        if i>maxi
            i=1;
        end
        if k<=0
            k=maxk;
        end
        if k>maxk
            k=1;
        end
        
        imshow(imresize(vol(:,:,i,k),2),[])
        title(strcat('Slice: ',num2str(i),'(',num2str(maxi),') Timepoint: ',num2str(k),'(',num2str(maxk),')'));

    end
end

Subject: How to close a figure without critical error

From: Jan Simon

Date: 3 Aug, 2010 17:22:09

Message: 2 of 4

Dear Austin,

> The problem is, I cannot figure out how to make it so that given a certain keystroke (for example, pressing enter), the figure will close and the function will terminate. I tried to put a bit of code like "if keystroke == enter, close", but that makes matlab crash.

> figure('KeyReleaseFcn',@cb)

Is there a reason to use the KeyRelease function and not the KeyPress function? As far as I can see, key press events in a figure without defined KeyPressFcn activate the command window. I don't know why your window is closed by this.

Another idea: The WindowKeyPressFcn catches all key press events, even if any object inside the window is the current object.

Kind regards, Jan

Subject: How to close a figure without critical error

From: Austin

Date: 6 Aug, 2010 18:00:23

Message: 3 of 4


>
> Is there a reason to use the KeyRelease function and not the KeyPress function? As far as I can see, key press events in a figure without defined KeyPressFcn activate the command window. I don't know why your window is closed by this.
>
> Another idea: The WindowKeyPressFcn catches all key press events, even if any object inside the window is the current object.
>
> Kind regards, Jan

Thank you for the response. I think you misunderstood my question. I want to be able to close the figure, and terminate the script, but I am unable to. My window does not close. I will try to use the keypressfcn as you recommended.

Subject: How to close a figure without critical error

From: Maxx Chatsko

Date: 6 Aug, 2010 19:54:04

Message: 4 of 4

"Austin " <apb14@case.edu>

Had a similar problem. Here's my fix:

      variable=get(your_figure,'string') %which will return the number handle
     close(variable) %closes the figure specified, this way works for all images

   Hope it helps
   Maxx

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us