在显示绘图时,我需要从键盘输入一些内容。使用waitforbuttonpress()
功能,我可以检测是否按下了鼠标按钮或键盘上的某个键,但我无法获得选定的特定键/字符。
我需要一个从情节输出窗口工作的方法,而不是从文本控制台。
这是我目前为止写的:
clf;
colormap ("default");
filename_orig = "./orig.data"
load("-text", filename_orig, "M")
M
t = 1
h = imagesc (M(:,:,t));
title ("Test data");
xlabel ("x");
ylabel ("y");
# Loop across time
b = 0;
for i = 1:10
for t = 1:size(M,3)
title(["Test data; t = ", num2str(t)]);
set(h, 'cdata', M(:,:,t)) # update latest frame
pause(0.20) # keep >0 to ensure redraw
b = waitforbuttonpress();
if(b == 1)
break;
endif
end
if(b == 1)
break;
endif
end
我想删除外部循环,并允许根据用户的按键增加/减少t
变量。
顺便说一下,是否有可能删除双break
从嵌套循环退出?
理想情况下,这是我想要的:
# Loop across time
b = 0;
t = 0;
while(1)
title(["Test data; t = ", num2str(t)]);
set(h, 'cdata', M(:,:,t)) # update latest frame
pause(0.20) # keep >0 to ensure redraw
k = readKeyboard(); # pseudo function
if(k == '+')
t = t + 1;
endif
if(k == '-')
t = t - 1;
endif
if(t < 0)
t = max;
endif
if(t > max)
t = 0;
endif
end
您可以将WindowKeyPressFcn回调函数附加到图中,如:
global t
function my_cbf(object, event)
k = event.key;
if(k == '+')
t = t + 1;
end
...
end
figure_handle = gcf();
set(figure_handle, 'WindowKeyPressFcn', my_cbf)