根据用户输入在 MATLAB 中绘图

Plot in MATLAB based on User Input

我目前正在使用以下函数在 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 上的文档似乎对我没有帮助。

您将需要指定一个 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