在matlab中检测击键,同时查看图形



我试图在matlab中对一些图像数据进行排序,并希望提示用户输入一系列图像。每次显示图像时,我都希望暂停并等待击键,并根据按下的内容执行不同的操作。

当前最佳解决方案:

responses = zeros(length(images),1);
for i = 1:length(images)
    im = imread(images{i}.fname);
    h = figure(1);
    imshow(im);
    % instead of just pause, I want to get the keystroke (k) that was pressed
    waitforbuttonpress;
    k = get(h,'CurrentCharacter');
    switch lower(k)
        case 'a'
            responses(i) = 1;
        case 'b'
            responses(i) = 2;
    end
end

您可以使用图形环境的KeyPressFcn属性,该属性应设置为回调函数的句柄,该函数接收包含按下字符的事件结构。有关详细信息和示例,请参阅Matlab文档。

您也可以查看图形环境的CurrentKey属性,但它无法告诉您何时实际按下了键

最新更新