2016-06-30 51 views

回答

0

我不太确定,你正在尝试做什么,但在下面找到一个小例子。 诀窍是使用回调函数:

http://de.mathworks.com/help/matlab/creating_plots/callback-definition.html

function test() 

global img1; 
global img2; 
global img3; 

img1 = imread(['icons' filesep 'calculation.png']); 
img2 = imread(['icons' filesep 'calibration.png']); 
img3 = imread(['icons' filesep 'gearwheels.png']); 


fh=figure; 
subplot(2,1,1),plot(rand(20)); 
subplot(2,1,2),plot(rand(10)); 

set(fh,'windowscrollWheelFcn', @showImage); 
set(fh,'Windowbuttonupfcn', 'gca'); 

end 

function showImage(~,~) 

persistent ind; 

global img1; 
global img2; 
global img3;  

if isempty(ind) || ind > 3 
    ind = 1; 
else 
    ind = ind + 1; 
end; 

switch ind 
    case 1 
     imshow(img1); 
    case 2 
     imshow(img2); 
    case 3 
     imshow(img3); 
    otherwise 
     imshow(img1); 
end; 

end 

我创建两个次要情节的人物。当你按次要情节的一个鼠标键,一个选择:

set(fh,'Windowbuttonupfcn', 'gca');

当您滚动鼠标滚轮的功能showImage叫:

set(fh,'windowscrollWheelFcn', @showImage);

此功能为你做所有的工作。 在这个例子中,我使用一个持久变量来切换加载的图像,这些图像在函数test中加载并存储在三个全局变量中。

它也可以给额外的参数功能showImage,交出请求图像的指标:

set(fh,'windowscrollWheelFcn', {@showImage, ind}); function showImage(~,~,ind)