matlab - Switching values to plot using keyboard input -


i have sets of data in matrix. want plot on set , use keyboard input move one. it's possible way:

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

but want move forward , backward in time t (e.g. using arrows). ok, done this:

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

but isn't there more elegant? (on 1 click without getting figure of sight - it's big full sreen figure.)

you can use mouse clicks combined ginput. can put code in while loop , wait user click somewhere on screen. ginput pauses until user input has taken place. must done on figure screen though. when you're done, check see key pushed act accordingly. left click mean plot next set of data while right click mean plot previous set of data.

you'd call ginput way:

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

x , y denote x , y coordinates of action occurred in figure window , b button pushed. don't need spatial coordinates , can ignore them when you're calling function.

the value of 1 gets assigned left click , value of 3 gets assigned right click. also, escape (on computer) gets assigned value of 27. therefore, have while loop keeps cycling , plotting things on mouse clicks until push escape. when escape happens, quit loop , stop asking input.

however, if want use arrow keys, on computer, value of 28 means left arrow , value of 29 means right arrow. i'll put comments in code below if desire use arrow keys.

do this:

%// 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 decide quit... while true      %// button user     [~,~,b] = ginput(1);      %// left click     %// use left arrow     %// if b == 28     if b == 1         %// check make sure don't go out of bounds         if ii < size(data,2)             ii = ii + 1; %// move right         end                             %// right click     %// use right arrow     %// elseif b == 29     elseif b == 3         if ii > 1 %// again check out of bounds            ii = ii - 1; %// move left         end     %// check escape     elseif b == 27        break;     end      %// plot new data     plot(data(:, ii), 'b.');     title(['data set #' num2str(ii)]); end 

here's animated gif demonstrating use:

enter image description here


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -