【发布时间】:2016-06-30 11:04:41
【问题描述】:
我在 matlab(R2015a) 中创建指南 gui(不是以编程方式)。它包含一个按钮来加载图像和轴来显示图像。当我加载图像时,我想通过鼠标滚动来更改轴图片。
有没有办法通过鼠标滚轮换图片?
问候。
【问题讨论】:
我在 matlab(R2015a) 中创建指南 gui(不是以编程方式)。它包含一个按钮来加载图像和轴来显示图像。当我加载图像时,我想通过鼠标滚动来更改轴图片。
有没有办法通过鼠标滚轮换图片?
问候。
【问题讨论】:
我不太确定您要做什么,但请在下面找到一个小示例。 诀窍是使用回调函数:
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)
【讨论】: