From: Austin on
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(a)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
From: Jan Simon on
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
From: Austin on

>
> 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.
From: Maxx Chatsko on
"Austin " <apb14(a)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