【问题标题】:How to make MATLAB detect keyboard stroke (arrows) and record the data?如何让 MATLAB 检测键盘敲击(箭头)并记录数据?
【发布时间】:2017-04-15 13:13:08
【问题描述】:

我正在尝试制作 MATLAB 代码,当有图形时检测键盘的左右箭头键并记录击键。

double(get(gcf,'currentcharacter'))

我尝试了上述功能,但我认为它不是我要寻找的功能。

【问题讨论】:

    标签: matlab keyboard keystroke


    【解决方案1】:

    你可以使用 ginput

    [~,~,button]=ginput(1);
    switch button
        case 30 %up
        case 31 %down
        case 28 %left
        case 29 %right
    end
    

    【讨论】:

    • 谢谢。它有效,但我怎样才能使“十字准线”不可见?使用前没有十字准线,但现在出现了。
    • 您可以编辑 ginput.m。在我的 matlab (2016b) 中是第 260 行: set(crossHair, 'Visible', 'on');如果您将其更改为 set(crossHair, 'Visible', 'off');十字准线不见了。此外,如果您注释掉第 210 行 set(gcf,'Pointer','custom','PointerShapeCData',cdata,'PointerShapeHotSpot',hotspot) 指针仍然可见。
    • mendi bucket 下面的解决方案比较优雅。您为按键后应该发生的事情编写自己的函数。但是改编 ginput 并将其重命名为 myginput.m 很简单。
    • 谢谢。还有一个问题。那么如果我修改 ginput.m 并将我的文件发送给其他人,那么它仍然适用吗?
    • 不,因为 ginput.m 是一个 matlab 函数。其他人在他们的matlab中有原始功能。建议不要将其保存为 ginput.m,而是保存为“myginput.m”,并在您的程序中引用它:[~,~,button]=myginput(1);当您将其发送给某人时,您会包含 myginput.m
    【解决方案2】:

    我过去为交互式缩放和平移数字写的东西:

    set(gcf,'WindowKeyPressFcn',{@KeyPressd,mean(xlim)});
    

    在“KeyPressd.m”中:

      function [  ] = KeyPressd( src,evnt,S0 )
        switch evnt.Key
            case 'f'
                set(gca,'XLim', xlim + range(xlim)/100 );
            case 'v'
                set(gca,'XLim', xlim - range(xlim)/100 );
            case 'h'
                set(gca,'YLim', ylim + range(ylim)/100 );
            case 'n'
                set(gca,'YLim', ylim - range(ylim)/100 );
            case 'g'
                set(gca,'ZLim', zlim + range(zlim)/100 );
            case 'b'
                set(gca,'ZLim', zlim - range(zlim)/100 );
    
            case 'd'
                set(gca,'XLim', xlim - 0.02*(xlim-sum(xlim)/2) );
            case 'c'
                set(gca,'XLim', xlim + 0.02*(xlim-sum(xlim)/2) );
            case 'a'
                set(gca,'YLim', ylim - 0.02*(ylim-sum(ylim)/2) );
            case 'z'
                set(gca,'YLim', ylim +  0.02*(ylim-sum(ylim)/2)  );
            case 's'
                set(gca,'ZLim', 0.98*(zlim) );
            case 'x'
                set(gca,'ZLim', 1/0.98*(zlim) );
    
        end
        set(gca,'ZTickLabel',sprintf('%1.0f\n',get(gca,'ZTick')));
    

    【讨论】:

      猜你喜欢
      • 2012-12-09
      • 2021-12-15
      • 2011-08-16
      • 2021-11-08
      • 1970-01-01
      • 2011-05-30
      • 1970-01-01
      • 2016-11-11
      • 2015-07-25
      相关资源
      最近更新 更多