使用键盘输入切换值以进行绘图

Vic*_*ira 5 matlab octave matlab-figure

我有一组矩阵中​​的数据.我想在集合上绘图,然后使用键盘输入移动到另一个.这种方式很简单:

for t=1:N
  plot(data(:,t))
  pause
end
Run Code Online (Sandbox Code Playgroud)

但我想及时前进t(例如使用箭头).好的,可以这样做:

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

但是有没有更优雅的东西?(只需点击一下即可获得视线图 - 这是一个很大的全屏.)

ray*_*ica 7

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

你这样打电话ginput:

[x,y,b] = ginput(1);
Run Code Online (Sandbox Code Playgroud)

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

为左侧单击分配值1,为右侧单击分配值3.另外,转义(在我的计算机上)被赋值为27.因此,你可以有一个while循环来保持循环并在鼠标点击上绘制内容,直到你推动逃脱.当转义发生时,退出循环并停止询问输入.

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

做这样的事情:

%// 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
Run Code Online (Sandbox Code Playgroud)

这是一个动画GIF演示它的用途:

在此输入图像描述


Hok*_*oki 6

此演示向您展示如何使用键盘的左右箭头切换数据集甚至鼠标滚轮.

它使用图的KeyPressFcn和/或WindowScrollWheelFcn事件.

function h = change_dataset_demo

%// sample data
nDataset = 8 ;
x = linspace(0,2*pi,50).' ;     %'// ignore this comment
data = sin( x*(1:nDataset) ) ;

index.max = nDataset ;
index.current = 1 ;

%// Plot the first one
h.fig = figure ;
h.plot = plot( data(:,index.current) ) ;

%// store data in figure appdata
setappdata( h.fig , 'data',  data )
setappdata( h.fig , 'index', index )

%// set the figure event callbacks
set(h.fig, 'KeyPressFcn', @KeyPressFcn_callback ) ;     %// Set figure KeyPressFcn function
set(h.fig, 'WindowScrollWheelFcn',@mouseWheelCallback)  %// Set figure Mouse wheel function

guidata( h.fig , h )


function mouseWheelCallback(hobj,evt)
    update_display( hobj , evt.VerticalScrollCount )


function KeyPressFcn_callback(hobj,evt)
    if ~isempty( evt.Modifier ) ; return ; end  % Bail out if there is a modifier

    switch evt.Key
        case 'rightarrow'
            increment = +1 ;
        case 'leftarrow'
            increment = -1 ;
        otherwise
            % do nothing
            return ;
    end
    update_display( hobj , increment )


function update_display( hobj , increment )

    h = guidata( hobj ) ;
    index = getappdata( h.fig , 'index' ) ;
    data  = getappdata( h.fig , 'data' ) ;

    newindex = index.current + increment ;
    %// roll over if we go out of bound
    if newindex > index.max
        newindex = 1 ;
    elseif newindex < 1
        newindex = index.max ;
    end
    set( h.plot , 'YData' , data(:,newindex) ) ;
    index.current = newindex ;
    setappdata( h.fig , 'index', index )
Run Code Online (Sandbox Code Playgroud)

当达到数据集的末尾时,这将翻转.

做了一点gif,但它不那么令人印象深刻,因为它没有显示键盘/鼠标动作,只有图表更新:

animgif

  • 我使用LICEcap捕获我的动画GIF:http://www.cockos.com/licecap/ - 将图形窗口放在录制区域内,单击开始录制,然后用鼠标点击指定FPS中的所有内容展示.它是免费的,适用于Windows和Mac OS ....也是开源的! (3认同)