MATLAB Data Cursor Mode - matlab

I have a simple class that plots basic x and y data. Within the class I have a method that enables the data cursor mode, customizes the text, and collects and saves the points. I'd like to change the behavior of the method so that I can collect only two points at a time. Right now it stores every point even when I turn off the data cursor mode and turn it back on to use it. Here is my code for my class:
classdef CursorPoint
properties
Xdata
Ydata
end
methods
function me = CursorPoint(varargin)
x_data = 0:.01:2*pi;
y_data = cos(x_data);
f= figure;
plot(x_data,y_data);
me.DCM(f);
end
function DCM(me,fig)
dcm_obj = datacursormode(fig);
set(dcm_obj,'UpdateFcn',#myupdatefcn)
set(dcm_obj,'enable','on')
myPoints=[];
function txt = myupdatefcn(empt,event_obj)
% Customizes text of data tips
pos = get(event_obj,'Position');
myPoints(end + 1,:) = pos;
txt = {['Time: ',num2str(pos(1))],...
['Amplitude: ',num2str(pos(2))]};
end
end
end
end

Could you change the myPoints variable to two variables called myPointCurrent and myPointPrevious. When ever the myupdatefcn method is called you would move the contents of myPointCurrent into myPointPrevious and then store the current position in myPointCurrent.
The new function (with some error checking) would look something like:
function txt = myupdatefcn(empt,event_obj)
% Customizes text of data tips
myPointPrevious=myPointCurrent;
pos = get(event_obj,'Position');
myPointCurrent=pos;
txt = {['Time: ',num2str(pos(1))],...
['Amplitude: ',num2str(pos(2))]};
end

Related

How to do an animate plot in MATLAB from a sequence of matrices

I have code that uses Wolff's Algorithm to simulate the XY Model in MATLAB and I want to implement a pcolor/color map to demonstrate each spin according to their angles across the system. But I want it to be live and changing as the angles change.
Any idea how to do this?
This is an example of how I want it to look https://i.stack.imgur.com/aSp7s.png
If you save each snapshot of the lattice in a cell array A{t}, you can use the following function to view and save it as a video (if fileName is not empty, the function saves an mp4 video).
Another option is to adapt the function view_lattice to run your simulation (which, honestly, I wouldn't recommend, for performance issues). I will mark where you should edit for doing a "live" simulation
This is at least MATLAB R2019b (although it may be compatible with earlier versions, but no guarantee).
File view_lattice.m
function view_lattice(A,fileName)
% for a 'live' simulation, you will have to remove A from the input
% parameters and add the ones you need for the XY Wolff algorithm,
% which will be used to calculate each configuration A in the time loop below
% you will also need to remove the assert statements for 'live' simulation
%
% otherwise, you save snapshots from your simulation
% and use this function as is
%
% A -> A{k}[m,n] snapshot k containing the angles of spins in lattice site at row m and col n
% fileName -> if contains string, then records a video with the snapshots and name it with this string
assert(iscell(A) && all(cellfun(#(a)isnumeric(a) && ismatrix(a),A)),'A must be cell of numeric matrices');
assert(ischar(fileName),'fileName must be either an empty char or contain a file name');
recordVideo = ~isempty(fileName);
if recordVideo
vw = setup_video(fileName);
else
vw = [];
end
% setting some default axis properties to speed-up plotting
set(0,'DefaultAxesPlotBoxAspectRatio',[1 1 1],'DefaultAxesDataAspectRatioMode','manual','DefaultAxesDataAspectRatio',[1,1,1],'DefaultAxesNextPlot','replace');
fh = figure;
ax=axes;
for t = 1:numel(A) % for 'live' simulation, this loop should be the time loop
% here you calculate the new configuration A
% and call the function below with A instead of A{t}
vw = record_frame(vw,fh,ax,A{t},t,recordVideo);
end
% any video to close?
if recordVideo
vw.close();
end
end
function vw = record_frame(vw,fh,ax,A,t,recordVideo)
imagesc(ax,A);
title(ax,sprintf('snapshot %g',t)); % if you want, y
axis(ax,'square');
daspect(ax,[1,1,1]);
pause(0.01);
if recordVideo
vframe = getframe(fh);
vw.writeVideo(vframe);
end
end
function vw = setup_video(fileName)
vid_id = num2str(rand,'%.16g');
vid_id = vid_id(3:6);
vid_id = [fileName,'_',vid_id];
% Initialize video
vw = VideoWriter([vid_id,'.mp4'], 'MPEG-4'); %open video file
vw.Quality = 100;
vw.FrameRate = 16;
vw.open();
end
Test script: test.m
clearvars
close all
A = cell(1,30);
for t = 1:numel(A)
% creating a sequence of random snapshots only for illustration
A{t} = rand(20,20);
end
% viewing the animation and saving it as a video with name test
view_lattice(A,'test');
Output

How to print the current filename in a plot in matlab?

I have one .m file per plot and want to see in my draft printouts, which file was used to create it.
This should be done with a function which can be placed in my .m file and
commented out, for the final version.
% addWatermarkFilename() %
So far I found mfilename(), but it could not get the name of the calling function. I am looking also for a good way to put the text in the picture without changing the size.
Solution:
I combined the suggestions by Luis Mendo and NKN to:
function [ output_args ] = watermarkfilename( )
% WATERMARKFILENAME prints the filename of the calling script in the
% current plot
s = dbstack;
fnames = s(2).name;
TH = text(0,0,fnames,'Interpreter','none');
TH.Color = [0.7 0.7 0.7];
TH.FontSize = 14;
TH.Rotation = 45;
uistack(TH,'bottom');
end
I suggest instead of commenting out a function from the code, use a proper flag. For instance the code can be something like this:
clc,clear,close all
addWaterMark = true; % if true you will get the filename in the title
fname = mfilename; % get file name mfilename('fullpath') for the full-path
t=-2*pi:0.1:2*pi;
y = sin(t);
plot(t,y); grid on;
xlabel('t');ylabel('y');
if addWaterMark
title(['filename: ' fname '.m']);
else
title('plot no.1');
end
A little bit playing with the text function, you can make a proper watermark. something like this:
clc,clear,close all
addWaterMark = true;
fname = mfilename;
fnames = [fname '.m'];
t=-2*pi:0.1:2*pi;
y = sin(t);
plot(t,y); grid on;
xlabel('t');ylabel('y');
if addWaterMark
title(['filename: ' fnames]);
t = text(-3,-0.4,fnames);
t.Color = [0.7 0.7 0.7];
t.FontSize = 40;
t.Rotation = 45;
else
title('plot no.1');
end
Note: Obviously the code between the if and else can be stored as a function that receives the string (char) from the fnames variable and a handle for the figure.
If you want to get the name of the function or script that called the current function or script, use dbstack as follows:
s = dbstack; % get struct with function call stack information
callerName = s(2).name; % get name of second function in the stack
The first entry in s refers to the current function; the second refers to the one that called it.

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])

Set data tips programmatically?

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)

Matlab fminsearch get's stuck in first iteration

I'm doing an optimization on a simulated robot's walk with the fminsearch method. What I did is create a function (the objective function) that writes to a text file the walking parameters (the input of fminsearch), opens the simulator (in webots) which writes the result of the walk in a text file and closes itself and then the objective returns the value in that text file. To sum up- the objective function get's a 8x12 matrix of leg position's and returns a scalar which indicates how good the walk is.
This works and the position values really change each iteration and the objective value improved indeed.
But here is the problem- I want to follow the function's value in each iteration (preferably by plot) and when I do that I get only the function's value at the first iteration and I can't understand why.
Here's the code:
options= optimset( 'PlotFcns', #optimplotfval);
[position,fval] = fminsearch(#callWebots,pos_start,options);
I tried displaying the results too and the same problem occurred (it displayed only the first iteration):
options= optimset(options, 'Display', 'iter-detailed');
I even tried to write an output function which will plot the fval and occurred with the same problem.
I would be grateful if you have any ideas why this can be...
Thank you in advanced
Here's the objective function:
function [objFun]=callWebots(pos)
%Open Motion File
filePath= fullfile('C:\Users\student\Documents\Webots\worlds', 'motion_trot_opt.motion');
data=convertToData(pos,filePath);
fout=fopen(filePath,'w');
%Write Motion File
for row=1:size(data,1)
for col=1:size(data,2)
if(col>1)
fprintf(fout, ',');
end
fprintf(fout, '%s', data{row,col});
end
fprintf(fout,'\n');
end
fclose(fout);
system('C:\Users\student\Documents\Webots\worlds\robot_full_withGPSLighter.wbt');
% Get result and return to main function
resfilePath= fullfile('C:\Users\student\Documents\Webots\worlds', 'result.txt');
fres=fopen(resfilePath);
result = textscan(fres, '%f');
fclose(fres);
objFun=cell2mat(result);
And the call for the objective function:
function [position,fval]=optCall()
%Read Initial Motion File Into CELL
filePath= fullfile('C:\Users\student\Documents\Webots\worlds', 'motion_trot_opt.motion');
fin=fopen(filePath);
ii = 1;
while 1
tline = fgetl(fin);
if (tline == -1)
break;
end
SplitedRow(ii,:) = regexp(tline, ',', 'split');
ii = ii+1;
end
fclose(fin);
%Convert from double to Data
[n,m] = size(SplitedRow);
pos_start = cellfun(#str2double,SplitedRow(2:end,3:end));
options= optimset( 'PlotFcns', #optimplotfval);
options= optimset(options, 'Display', 'iter-detailed');
[position,fval] = fminsearch(#callWebots,pos_start,options);