Set data tips programmatically? - matlab

I need to be able to set data tips programmatically from a list of array of x axis values. For example, I create a figure and plot my data.
figure;plot(t1,[filter(b,a,Gyro(:,2)),filter(b,a,Gyro(:,4))])
I have a set of timestamp values from t1 variable (time) (e.g. [0.450, 0.854, 1.2343....]) where I want to place data tips to mark certain events in my data. Without having to place them every time manual by clicking and saving data trip... How can I pass them as array and do this programmatically through matlab script?

You can add matlab datatip programatically and customize them to an extent.
The function below shows how to add a few datatip, position them and customize their display:
The code for this demo (save that in a file demo_datatip.m and run it to obtain the above figure) :
function h = demo_datatip
%// basic sample curve
npts = 600 ;
x = linspace(0,4*pi,npts) ;
y = sin(x) ;
%// plot
h.fig = figure ;
h.ax = axes ;
h.plot = plot(x,y) ;
%// simulate some event times
time_events = x([25 265 442]) ; %// events type 1 at index 25, 265 and 422
%// define the target line for the new datatip
hTarget = handle(h.plot);
%// Add the datatip array
h.dtip = add_datatips( time_events , hTarget ) ;
function hdtip = add_datatips( evt_times , hTarget )
%// retrieve the datacursor manager
cursorMode = datacursormode(gcf);
set(cursorMode, 'UpdateFcn',#customDatatipFunction, 'NewDataCursorOnClick',false);
xdata = get(hTarget,'XData') ;
ydata = get(hTarget,'YData') ;
%// add the datatip for each event
for idt = 1:numel(evt_times)
hdtip(idt) = cursorMode.createDatatip(hTarget) ;
set(hdtip(idt), 'MarkerSize',5, 'MarkerFaceColor','none', ...
'MarkerEdgeColor','r', 'Marker','o', 'HitTest','off');
%// move it into the right place
idx = find( xdata == evt_times(idt) ) ;%// find the index of the corresponding time
pos = [xdata(idx) , ydata(idx) ,1 ];
update(hdtip(idt), pos);
end
function output_txt = customDatatipFunction(~,evt)
pos = get(evt,'Position');
idx = get(evt,'DataIndex');
output_txt = { ...
'*** !! Event !! ***' , ...
['at Time : ' num2str(pos(1),4)] ...
['Value: ' , num2str(pos(2),8)] ...
['Data index: ',num2str(idx)] ...
};
If you need to delete a data tip, you can simply call delete(datatip_handle) on it's handle (or even an array of handles to delete them in group).

Here is a small example that might be what you are looking for:
time = 0:0.5:10;
values = rand(1,length(time));
datatip_index = [3 6]; % an 'event' occurs on the 3rd and 6th datapoint
datatip_text = {'event1', 'event2'};
figure;
plot(time,values)
hold on
text(time(datatip_index), values(datatip_index), datatip_text)

Related

Updating data cursor position within a loop

I have a function updating a plot given a new point (it appends the last point to the line). I want to have the possibility to update the cursor such that it automatically appears on the last point. Currently, I do
for i = 1 : numel(dataObjs)
X{i}(end+1) = newX{i};
Y{i}(end+1) = newY{i};
set(dataObjs(i), 'XData', X{i});
set(dataObjs(i), 'YData', Y{i});
set(cursorMode, 'enable', 'on');
% Create a new data tip
hDatatip = cursorMode.createDatatip(dataObjs(i));
% Update annotation position
hDatatip.Cursor.Position = [newX{i}, newY{i} 0];
end
However, it is slow since it always creates a new cursor. I cannot find where the old one is stored such that I don't have to create new ones.
Rather than creating a new cursor object every time you add new data, you could create it once (per plot object) and save it to a variable. Then inside of the loop you can update the position.
set(cursorMode, 'Enable', 'on')
%// Create all of your data cursors once
for k = 1:numel(dataObjs)
datacursors(k) = cursorMode.createDatatip(dataObjs(k));
end
%// Now when you add new data
for k = 1 : numel(dataObjs)
X{k}(end+1) = newX{k};
Y{k}(end+1) = newY{k};
set(dataObjs(k), 'XData', X{k});
set(dataObjs(k), 'YData', Y{k});
%// Update annotation position
datacursors(k).Cursor.Position = [newX{k}, newY{k} 0];
end
And for the sake of a complete example:
hfig = figure();
data = rand(5,4);
hplots = plot(data);
cursorMode = datacursormode(hfig);
for k = 1:numel(hplots)
datacursors(k) = cursorMode.createDatatip(hplots(k));
end
for k = 1:size(data, 1)
for m = 1:numel(hplots)
set(datacursors(m), 'Position', [k, data(k,m)])
end
end
Update
As an alternative you could use findall to actually locate the data cursor for a given plot. The only downside of this is that it adds the overhead of having to find the data cursor every time that you want to update it.
datacursor = findall(ancestor(hplots(k), 'axes'), 'DataSource', hplots(k));
Another alternative
Rather than storing everything within a variable, you could store the data cursor in the UserData property of the plot objects themselves.
for k = 1:numel(hplots)
set(hplots(k), 'UserData', cursorMode.createDatatip(hplots(k)))
end
Then to use it:
set(get(hplots(k), 'UserData'), 'Position', [x, y, 0])

how do i mask labeled object based on some specified threshold value for each objects area,majoraxis and minoraxis?

i am currently using bwconnomp to label each connected object and regionpropsto find area, majoraxis, minoraxis of each labeled object respectively. i am also displaying each labeled object its area,majoraxis and minoraxis. now i want to set some threshold for area,majoraxis and minoraxis and if the value of area,majoraxis and minoraxis is above specified threshold then that object has to be masked.how this can be done??
here is my code
clc
clear all
close all
Index = 1;
scrsz = get(0,'ScreenSize');
%read an image
while Index ~= 0
% Open a dialog and select an image file
[FileName,FilePath,Index] = uigetfile('*.png', 'Open Imagefile ');
if Index == 0
disp('Procedure Done')
break;
end
inimage = imread([num2str(FilePath) FileName]);
D=inimage;
A=inimage;
subplot(2,3,1);
imshow(inimage);
title('original image');
%labeling algorithm
B=im2bw(inimage);
C=imfill(B,'holes');
label=bwlabel(C);
max(max(label))
CC = bwconncomp(B);
data = regionprops(CC,'all');
for j=1:max(max(label))
[row, col] = find(label==j);
len=max(row)-min(row)+2;
breadth=max(col)-min(col)+2;
target=uint8(zeros([len breadth] ));
sy=min(col)-1;
sx=min(row)-1;
for i=1:size(row,1)
x=row(i,1)-sx;
y=col(i,1)-sy;
target(x,y)=A(row(i,1),col(i,1));
end
mytitle=strcat('Object Number:' ,num2str(j),'area:', num2str(data(j).Area),'MajorAxis: ',num2str(data(j).MajorAxisLength),'MinorAxis: ',num2str(data(j).MinorAxisLength));
figure,imshow(target);title(mytitle);
a=size(target);
ax=a(1);
ay=a(2);
pos=[1,1,ay,ax];
rectangle('Position',pos,'EdgeColo','r')
end
next = input('next image? press Enter: ');
if next == 0
channelactivity = 0;
break
else
close all
disp('==================================')
pause(0.2)
continue
end
end
Here is a way to do it. The code is commented so easy to follow; the important line is the following:
AboveAreaIndices = find(vertcat(data.Area) > SomeValue)
In which you store the indices of the objects whose area is larger than SomeValue. In the example I color them red but you can do whatever you want with them or remove them altogether from the data structure.
You can also use logical operators to combine multiple conditions for example using the MinorAxis and MajorAxis properties. Note that I used AllArea as anew variable to store the concatenated areas to make things clearer, but you can keep them as vertcat(data.Area).
AboveIndices = find(vertcat(data.Area) > SomeValue & vertcat(data. MinorAxis) > SomeValue & Bla bla bla...);
Whole code:
clear
clc
close all
%// Read and clean up sample image
A = imread('rice.png');
A = im2bw(A,.5);
A = bwareaopen(A,50);
CC = bwconncomp(A);
%// Same as you.
data = regionprops(CC,'all');
%// Concatenate all the areas into an array.
AllArea = vertcat(data.Area);
%//========================================
%//==== Apply threshold on area here \\====
AboveAreaIndices = find(AllArea > 150);
%// If you wish to remove the entries from the data structure
% data(AllArea>150) = [];
%//========================================
%// Same for centroids...for display purposes
AllCentroids = vertcat(data.Centroid);
%// Display original and thresholded objects. Use the indices calculated
%// above to "mask" large areas if you want
imshow(A);
hold on
scatter(AllCentroids(:,1),AllCentroids(:,2),40,'b','filled')
scatter(AllCentroids(AboveAreaIndices,1),AllCentroids(AboveAreaIndices,2),40,'r','filled')
And sample output:

Switching values to plot using keyboard input

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
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.)
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.
You'd call ginput this way:
[x,y,b] = ginput(1);
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.
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.
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.
Do something like 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 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
Here's an animated GIF demonstrating its use:
This demo shows you how to use either the left and right arrows of the keyboard to switch data set or even the mouse wheel.
It uses the KeyPressFcn and/or WindowScrollWheelFcn event of the figure.
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 )
This will roll over when the end of the data set is reached.
done a little gif too but it's a lot less impressive because it does not show the keyboard/mouse action, only the graph updates :

For Loop and indexing

Following is my code :
function marks(my_numbers)
handle = zeros(5,1)
x = 10 ;
y = 10:20:100 ;
for i = 1
for j = 1:5 ;
handle(j,i) = rectangle('position',[x(i),y(j),20 10],'facecolor','r')
end
end
end
now lets say input argument my_numbers = 2
so i have written the code :
set(handle(j(my_numbers),1),'facecolor','g')
With this command, rectangle with lower left corner at (30,10) should have turned green. But MATLAB gives an error of index exceeds matrix dimensions
This is more an illustrated comment than an answer, but as #hagubear mentioned your i index is pointless so you could remove it altogether.
Using set(handle(my_numbers,1),'facecolor','g') will remove the error, because you were trying to access handles(j(2),1) and that was not possible because j is a scalar.
Anyhow using this line after your plot works fine:
set(handle(my_numbers,1),'facecolor','g')
According to your comment below, here is a way to call the function multiple times and add green rectangles as you go along. There are 2 files for the purpose of demonstration, the function per se and a script to call the function multiple times and generate an animated gif:
1) The function:
function marks(my_numbers)
%// Get green and red rectangles to access their properties.
GreenRect = findobj('Type','rectangle','FaceColor','g');
RedRect = findobj('Type','rectangle');
%// If 1st call to the function, create your plot
if isempty(RedRect)
handle = zeros(5,1);
x = 10 ;
y = 10:20:100 ;
for j = 1:5 ;
handle(j) = rectangle('position',[x,y(j),20 10],'facecolor','r');
end
set(handle(my_numbers,1),'facecolor','g')
%// If not 1st call, fetch existing green rectangles and color them green. Then color the appropriate rectangle given by my_numbers.
else
RedRect = flipud(RedRect); %// Flip them to maintain correct order
if numel(GreenRect) > 0
hold on
for k = numel(GreenRect)
set(GreenRect(k),'facecolor','g')
set(RedRect(my_numbers,1),'facecolor','g')
end
end
end
2) The script:
clear
clc
%// Shuffle order for appearance of green rectangles.
iter = randperm(5);
filename = 'MyGifFile.gif';
for k = iter
marks(k)
pause(1)
frame = getframe(1);
im = frame2im(frame);
[imind,cm] = rgb2ind(im,256);
if k == iter(1)
imwrite(imind,cm,filename,'gif', 'Loopcount',inf);
else
imwrite(imind,cm,filename,'gif','WriteMode','append');
end
end
Here is the animated gif of the output:

how to make Sliding window model for data stream mining?

we have a situation that a stream (data from sensor or click stream data at server) is coming with sliding window algorithm we have to store the last (say) 500 samples of data in memory. These samples are then used to create histograms, aggregations & capture information about anomalies in the input data stream.
please tell me how to make such sliding window.
If you are asking how to store and maintain these values in a sliding-window manner, consider this simple example which keep tracks of the running mean of the last 10 values of some random stream of data:
WINDOW_SIZE = 10;
x = nan(WINDOW_SIZE,1);
%# init
counter = 0;
stats = [NaN NaN]; %# previous/current value
%# prepare figure
SHOW_LIM = 200;
hAx = axes('XLim',[1 SHOW_LIM], 'YLim',[200 800]);
hLine = line('XData',1, 'YData',nan, 'EraseMode','none', ...
'Parent',hAx, 'Color','b', 'LineWidth',2);
%# infinite loop!
while true
val = randi([1 1000]); %# get new value from data stream
x = [ x(2:end) ; val ]; %# add to window in a cyclic manner
counter = counter + 1;
%# do something interesting with x
stats(1) = stats(2); %# keep track of the previous mean
stats(2) = nanmean(x); %# update the current mean
%# show and update plot
set(hLine, 'XData',[counter-1 counter], 'YData',[stats(1) stats(2)])
if rem(counter,SHOW_LIM)==0
%# show only the last couple of means
set(hAx, 'XLim', [counter counter+SHOW_LIM]);
end
drawnow
pause(0.02)
if ~ishandle(hAx), break, end %# break in case you close the figure
end
Update
The EraseMode=none property was deprecated and removed in recent versions. Use the animatedline function instead for a similar functionality.