基于用户输入在MATLAB中绘图

时间:2016-04-12 13:58:10

标签: matlab matlab-figure

我目前正在使用以下函数在MATLAB中绘制单个数字

PlotImage(finalImage, 1, 4, 4, 'Final Image');

function[] = PlotImage(image, y, x, value, name)
subplot(y,x,value);
imagesc(image);
axis image;
title(name);
end

我有几个' finalImages'我希望根据用户输入显示,即程序默认显示图像1,然后如果在键盘上按下按键1 - 5,它将再次使用不同的图像调用PlotImage(图像1 - 5)。

有办法做到这一点吗?关于KeyPressFcn的文档似乎对我没有帮助。

1 个答案:

答案 0 :(得分:0)

您需要指定处理按键事件的KeyPressFcn并调用所有必要的绘图命令(并且可能涉及调用PlotImage

hfig = figure('WindowKeyPressFcn', @(src,evnt)keypress(evnt));

%// Create 5 "images" to show
finalImages = {rand(4), rand(4), rand(4), rand(4), rand(4)};

function keypress(evnt)
    if ismember(evnt.Key, '12345')
        img = finalImages{str2double(evnt.Key)};
        PlotImage(img, 1, 4, 4, 'Final Image');
    end
end

function PlotImage(img, y, x, value, name)
    subplot(y,x,value);
    imagesc(img);
    axis image;
    title(name);
end
相关问题