使用键盘输入切换值以绘图 [英] Switching values to plot using keyboard input

查看:103
本文介绍了使用键盘输入切换值以绘图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在矩阵中有几组数据.我想在场景上绘制,然后使用键盘输入移动到另一个.这样很可能是这样:

I have sets of data in a matrix. I want to plot on set and then use a keyboard input to move to another one. It's simply possible this way:

for t=1:N
  plot(data(:,t))
  pause
end

,但是我想在时间t中向前和向后移动(例如,使用箭头).好,可以这样做:

but I want to move forward and backward in time t (e.g. using arrows). OK, it could be done like this:

direction = input('Forward or backward?','s')
if direction=='forward'
   plot(data(:,ii+1))
else
   plot(data(:,ii-1))
end

但是没有更优雅的东西吗? (只需单击一下即可获得清晰的身影-这是一个很大的全屏人物.)

but isn't there something more elegant? (On one click without getting the figure of the sight - it's a big full sreen figure.)

推荐答案

您可以将鼠标单击与 ginput .您可以做的是将代码放入while循环中,然后等待用户单击屏幕上的某个位置. ginput暂停,直到发生某些用户输入为止.但是,这必须在图形屏幕上完成.完成后,检查按下了哪个键,然后采取相应的措施.左键单击意味着您将绘制下一组数据,而右键单击将意味着您将绘制上一组数据.

You can use mouse clicks combined with ginput. What you can do is put your code in a while loop and wait for the user to click somewhere on the screen. ginput pauses until some user input has taken place. This must be done on the figure screen though. When you're done, check to see which key was pushed then act accordingly. Left click would mean that you would plot the next set of data while right click would mean that you plot the previous set of data.

您可以通过以下方式致电ginput:

You'd call ginput this way:

[x,y,b] = ginput(1);

xy表示图形窗口中发生动作的位置的xy坐标,并且b是您按下的按钮.实际上,您不需要空间坐标,因此在调用函数时可以忽略它们.

x and y denote the x and y coordinates of where an action occurred in the figure window and b is the button you pushed. You actually don't need the spatial coordinates and so you can ignore them when you're calling the function.

为1的值分配一个左键单击,为3的值分配一个右键单击.另外,(在我的计算机上)转义被分配了27的值.因此,您可能有一个while循环,该循环不断循环并通过单击鼠标来绘制内容,直到您按下转义为止.当发生转义时,退出循环并停止要求输入.

The value of 1 gets assigned a left click and the value of 3 gets assigned a right click. Also, escape (on my computer) gets assigned a value of 27. Therefore, you could have a while loop that keeps cycling and plotting things on mouse clicks until you push escape. When escape happens, quit the loop and stop asking for input.

但是,如果要使用箭头键,在我的计算机上,值28表示左箭头,值29表示右箭头.如果您想使用箭头键,请在下面的代码中添加注释.

However, if you want to use arrow keys, on my computer, the value of 28 means left arrow and the value of 29 means right arrow. I'll put comments in the code below if you desire to use arrow keys.

执行以下操作:

%// Generate random data
clear all; close all;
rng(123);
data = randn(100,10);

%// Show first set of points
ii = 1;
figure;
plot(data(:,ii), 'b.');   
title('Data set #1');  

%// Until we decide to quit...
while true 
    %// Get a button from the user
    [~,~,b] = ginput(1);

    %// Left click
    %// Use this for left arrow
    %// if b == 28
    if b == 1
        %// Check to make sure we don't go out of bounds
        if ii < size(data,2)
            ii = ii + 1; %// Move to the right
        end                        
    %// Right click
    %// Use this for right arrow
    %// elseif b == 29
    elseif b == 3
        if ii > 1 %// Again check for out of bounds
           ii = ii - 1; %// Move to the left
        end
    %// Check for escape
    elseif b == 27
       break;
    end

    %// Plot new data
    plot(data(:, ii), 'b.');
    title(['Data set #' num2str(ii)]);
end

以下是一个动画GIF,演示了其用法:

Here's an animated GIF demonstrating its use:

这篇关于使用键盘输入切换值以绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆