Matlab - updating objects on a plot - matlab

I am trying to add a timer a simulation i am working on. At the moment i can get the timer to display in the location i want, however i cant make the numbers clear each other, i.e they all just stack on top of each other slowly creating a solid black mess. I have tried implementing a clf function but it just clears the entire figure. The code for the timer is:
HH = 0; MM = 0; SS = 0;
timer = sprintf('%02d:%02d:%02d',HH,MM,SS);
text(-450,450,timer); %% adjust location of clock in graph using the first two arguments: (x,y) coordinates
for t = 1:86400
SS = SS + 1;
if SS == 60
MM = MM + 1;
SS = 0;
end
if MM == 60
HH = HH + 1;
MM = 0;
end
timer = sprintf('%02d:%02d:%02d',HH,MM,SS); %% construct time string after all adjustments to HH, MM, SS
clf(f,'reset'); %% clear previous clock display
text(-450,450,timer); %% re-plot time to figure
if t == EventTimes(1)
uav1 = uav1.uavSetDestin([event1(2:3) 0]);
plot(event1(2),event1(3),'+')
hold on
end
if t == EventTimes(2)
uav2 = uav2.uavSetDestin([event2(2:3) 0]);
plot(event2(2),event2(3),'r+')
hold on
end
Is there a way i can reset only the timer function so it displays properly?

You want to store the handle to the text object and update the String property of this existing object rather than creating a new text object every time.
%// The first time through your loop
htext = text(-450, 450, timer);
%// Every other time through the loop
set(htext, 'String', sprintf('%02d:%02d:%02d',HH,MM,SS)
You will also want to do something similar with the plot objects rather than clearing the figure and redrawing all of the plots every iteration.
Integrating this with your code we get something like:
%// Create the initial text object
HH = 0; MM = 0; SS = 0;
timerString = sprintf('%02d:%02d:%02d',HH,MM,SS);
htext = text(-450, 450, timeString);
%// Create the plot objects
hplot1 = plot(NaN, NaN, '+');
hplot2 = plot(NaN, NaN, 'r+');
for t = 1:86400
SS = SS + 1;
%// I could help myself and made this significantly shorter
MM = MM + (mod(SS, 60) == 0);
HH = HH + (mod(MM, 60) == 0);
%// Update the timer string
timerString = sprintf('%02d:%02d:%02d',HH,MM,SS);
set(htext, 'String', timerString);
%// Update your plots depending upon which EventTimes()
if t == EventTimes(1)
uav1 = uav1.uavSetDestin([event1(2:3) 0]);
set(hplot1, 'XData', event1(2), 'YData', event1(3));
elseif t == EventTimes(2)
uav2 = uav2.uavSetDestin([event2(2:3) 0]);
set(hplot2, 'XData', event2(2), 'YData', event2(3));
end
%// Force a redraw event
drawnow;
end

Related

How to update xtick and xticklabel in for loop?

Imagine a = 1:1000, b = sin(a) and we want to segment this data with segment_length = 100 and plot only each segment and shift the segment with segment_length/2 in for loop. I want to plot a figure in a for loop in which xtick and xticklabel update in each loop: 1st loop is 1-100, 2nd is 51-150, 3rd is 101-200 and so on. In the following code I just plot the interested data in each loop but xtick and xticklabel do not change.
a = 1:1000;
b = sin(a);
segment_length = 100;
shift = segment_length/2;
seg_no = 1000/shift;
start = 1;
en = segment_length;
for i = 1 : seg_no-1
seg = b(start : en)
plot(seg)
start = shift + start
en = shift + en
end
note that you are not providing any information of the x-coordinates to Matlab. Thus, it must assume that your index always starts at 1. That is why they don't change.
Here is the solution
a = 1:1000;
b = sin(a);
segment_length = 100;
shift = segment_length/2;
seg_no = 1000/shift;
start = 1;
en = segment_length;
for i = 1 : seg_no-1
idx = start:en;
seg = b(idx);
plot(idx,seg)
start = shift + start;
en = shift + en;
drawnow % force matlab to draw/update the figure
pause(0.1) % / s | pause for 0.1 seconds
end
Note that I used the commands drawnow forcing Matlab to update the figure before proceeding. I also added pause() to define how long Matlab should wait to better see the updated figure.

Plot graph with 2 variables in matlab

I'm planning to plot a graph of velocity against time using matlab. The change of time is 0.05 and total time 15. When time change, the graph will change and save a figure of that. I had mat file which contained all the data for time and velocity.
E.g, t=0, v=0, plot and save, t=0.05, v=1, plot and save until t=15.
I tried to use v=v+1 (which acts like i++) but failed to read the value of v in 2nd row. Any other method to do so?
Thank You.
The code is
i = 001
dt = t(2,1) - t(1,1);
k = dt*(i-1);
filename1 = 'front_data';
matFileName = sprintf('%s.mat', filename1);
matData = load(matFileName);
t = matData.time;
fv = matData.front_velocity;
fig = figure%('visible', 'off');
s = t(1,1);
fv = fv(1,1);
lot (s,fv,'*')
pic_filename = sprintf('front_data%02d.jpeg', k);
print(fig,pic_filename,'-djpeg')
istart = 002
iend = 301
for i = istart:iend
k = dt*(i-1);
t = t+dt
filename1 = 'front_data';
matFileName = sprintf('%s.mat', filename1);
matData = load(matFileName);
t = matData.time;
fv = matData.front_velocity;
v = fv(1,1);
v = v+1;
h = figure
axis([0 15 0 0.6])
plot(t,v,'*')
pic_filename = sprintf('front_data%02d.jpeg', k);
print(h,pic_filename,'-djpeg')
end
And the example I refer is the [https://www.mathworks.com/matlabcentral/answers/110632-how-to-increment-a-variable]
I reduced your example to the essential parts.
istart = 2;
iend = 301;
counter=istart;
%load data
% filename1 = 'front_data';
% matFileName = sprintf('%s.mat', filename1);
% matData = load(matFileName);
% t = matData.time;
% fv = matData.front_velocity;
%for demonstaration
t=0:.05:15;
fv=rand(size(t));
for i = istart:iend
%update
time = t(istart:counter);
values = fv(istart:counter);
%plot
plot(time,values,'*')
%increase index
counter=counter+1;
end
As you are loading always the same data in the loop you can do it once outside the loop, and for plotting you just update the length of your vector to be plotted. You could also just append the new value to the actual list.

How can i read and store mouse coordinates while the rest of the function does not do much, pretty much stall?

so i have a matlab function below that returns a matrix of [x y time_elapsed] coordinates from 'start' to 'target', and 'target' is reached when it is at distance of acc_length to it. the time_elapsed is to help me get the speed profile.
Currently, this works fine with sampling roughly every 0.01s and there is a 2-second pause when starting the motion from 'start'.
My question is how can i do the same thing - having the user to wait 2s once 'start' position is reached, but instead of pausing the program, how can I still read the mouse coordinates(within the 2s period) while nothing useful happens for 2s?
P.S. i want to get these coordinate to calculate the precision of user to the start.
function arr = pmove(start, target, acc_length)
uimenufcn(gcf,'WindowCommandWindow');
arr = [];
checkStart = 1;
checkEnd = 0;
while checkStart
pt = get(0,'PointerLocation');
pt = [pt(1) - 1920/2, pt(2) - 1080/2];
d = pdist( [start; pt] );
if ~checkEnd & d <= acc_length;
disp('Start moving to target after 2 seconds.')
pause(2);
pt = get(0, 'PointerLocation');
pt = [pt(1) - 1920/2, pt(2)-1080/2, 0];
arr = cat(1, arr, pt);
checkEnd = 1;
tic;
continue;
end
if checkEnd
d = pdist( [target; pt] );
t_elapsed = toc;
pt = [pt , t_elapsed];
arr = cat(1, arr, pt);
if d <= acc_length
checkStart = 0;
end
end
if ~isempty(arr)
figure(gcf)
hold on;
plot(arr(:,1), arr(:,2) );
%drawnow;
axis equal;
end
pause(0.01);
end
end
You can replace the pause(2) line with:
for i=1:200
pt2 = get(0, 'PointerLocation');
% do your calculation
pause(0.01);
end
i'm using another while-true loop and checking the time_elapsed, if it is longer than 2.1s (tolerance of 0.1s for the user) i break out of the loop. During which, i get the pointer cordinates about every 0.01s and run my calculations.
tic;
while 1
pt2 = get(0,'PointerLocation');
% calculation
time_elaps = toc;
if time_elaps > 2.1 % tolerance of 0.1s
break;
end
pause(0.01);
end

Matlab - displaying an object on a graph

I have a function for a timer as follows
HH = 0; MM = 0; SS = 0;
timer = sprintf('%02d:%02d:%02d',HH,MM,SS);
for p = 1:86400
SS = SS + 1;
if SS == 60
MM = MM + 1;
SS = 0;
pause(0.01)
end
if MM == 60
HH = HH + 1;
MM = 0;
pause(0.1)
end
HH;
end
disp(timer)
How can i get this to display on a graph while constantly updating. Have not been able to get it to work using the plot() or set() function.
You might want to adjust the particulars of this example based on how you want the timer to look, and notice that I just put a pause(0.1) for each second of the clock, so you can watch it faster than real time as indicated in your example. You can also adjust the size/color of the clock with different arguments to the text() function.
f = figure;
HH = 0; MM = 0; SS = 0;
timer = sprintf('%02d:%02d:%02d',HH,MM,SS);
text(0.5,0.5,timer); %% adjust location of clock in graph using the first two arguments: (x,y) coordinates
for p = 1:86400
SS = SS + 1;
pause(0.1); %% pause a fixed amount for each clock tick
if SS == 60
MM = MM + 1;
SS = 0;
end
if MM == 60
HH = HH + 1;
MM = 0;
end
timer = sprintf('%02d:%02d:%02d',HH,MM,SS); %% construct time string after all adjustments to HH, MM, SS
clf; %% clear previous clock display
text(0.5,0.5,timer); %% re-plot time to figure
end

Code for a multicolumn legend in Matlab

I have designed an application called CLegend, which allows to build a multicolumn legend, whose code is
function CLegend(hax,numcol,Ley)
%# Inputs
% hax : handle of the axes object to which belongs the legend
% numcol: number of columns for the legend
% Ley: text strings (labels) for the legend
set(hax,'Units','normalized','Position',[0.1 0.1 0.8 0.8]);
set(hax,'Units','characters');
posAx = get(hax,'Position');
insAx = get(hax,'TightInset');
[legend_h,object_h] = legend(hax,Ley,'Units','characters','Location',...
'South','Orientation','vertical');
posl = get(legend_h,'Position');
numlines = length(Ley);
if (numlines<numcol)
numcol = numlines;
end
numpercolumn = ceil(numlines/numcol);
if (mod(numlines,numpercolumn) == 0)
numcol = numlines/numpercolumn;
end
l = zeros(1,numlines);
a = zeros(1,numlines);
h = zeros(1,4);
for j=1:numlines
h = get(object_h(j),'Extent');
l(j) = h(3);
a(j) = h(4);
set(object_h(j),'Units','characters');
end
lmax = posl(3)*max(l);
hmax = posl(4)*max(a);
hLine = object_h(numlines+1);
xdata = get(hLine, 'xdata');
dx = xdata(2)-xdata(1);
di = 2;
sheight = hmax;
height = hmax*numpercolumn-sheight/2;
line_width = dx*posl(3);
spacer = xdata(1)*posl(3);
delta1 = spacer + line_width + spacer + lmax;
delta2 = line_width + spacer + lmax + spacer;
delta3 = lmax + spacer + line_width + spacer;
factx = 1/(posl(3)*numcol);
facty = 1/(hmax*numpercolumn);
width_l = numcol*delta1;
set(legend_h, 'Position', [posAx(1) + 0.5*(posAx(3)-width_l) posl(2) ...
width_l numpercolumn*hmax]);
col_ind = -1;
row_ind = -1;
j = 0;
for i=1:numlines,
if strcmpi(orient,'horizontal'),
if mod(i,numcol)==1,
row_ind = row_ind+1;
end
col_ind = mod(i,numcol)-1;
if col_ind == -1,
col_ind = numcol-1;
end
else
if numpercolumn==1 || mod(i,numpercolumn)==1,
col_ind = col_ind+1;
end
row_ind = mod(i,numpercolumn)-1;
if row_ind == -1,
row_ind = numpercolumn-1;
end
end
if (i==1)
linenum = i+numlines;
else
linenum = linenum+di;
end
labelnum = i;
set(object_h(linenum), 'ydata',facty*[height-row_ind*sheight ...
height-row_ind*sheight]);
set(object_h(linenum), 'xdata', factx*[spacer + j*delta2 ...
spacer + j*delta2 + line_width]);
set(object_h(linenum+1), 'ydata',facty*(height-row_ind*sheight));
set(object_h(linenum+1), 'xdata', factx*(spacer+line_width/2));
set(object_h(labelnum), 'Position', [j*delta3+spacer*2+line_width ...
height-row_ind*sheight]);
if (mod(i,numpercolumn)== 0)
j = j + 1;
end
end
opl = get(legend_h,'OuterPosition');
set(hax, 'Position',[posAx(1) posAx(2)+opl(4) posAx(3) posAx(4)-opl(4)]);
set(legend_h, 'OuterPosition',[opl(1) (posAx(2)-insAx(2))/2 opl(3) opl(4)]);
set([hax,legend_h],'Units','normalized');
end
I have problems with it. I have tried it with a more complicated code but similar to this one that appears below.
function qq
fh = figure('Units','characters');
GrapWin = uipanel ('Parent',fh,'Units','characters','title','Graphic',...
'Position',[135 5 120 40]);
haxes = axes('Parent',GrapWin,'Units','normalized','Position',[0.1 0.1 0.8 0.8]);
PanVD = uipanel('Parent',fh,'Units','characters','Position',[55 5 30 10],...
'title','Dependent Variable');
VDlh = uicontrol('Parent',PanVD,'Style','listbox','Units',...
'normalized','Position',[0 0 1 1],'String',{'X','Y','Z'},'Value',1,...
'Callback',#VDLhCallback);
T = 0:pi/100:2*pi;
X = sin(T);
xlabel(haxes,'Time');
title(haxes,'Sine Function');
plot(haxes,T,X);
CLegend(haxes,3,{'Var X'});
function VDLhCallback (src,evt)
value = get(VDlh,'Value');
switch value
case 1
title(haxes,'Sine Function');
plot(haxes,T,X);
CLegend(haxes,3,{'Var X'});
case 2
title(haxes,'Cosine Function');
Y = cos(T);
plot(haxes,T,Y);
CLegend(haxes,3,{'Var Y'});
case 3
Z = tan(T);
title(haxes,'Tangent Function');
plot(haxes,T,Z);
CLegend(haxes,3,{'Var Z'});
end
end
end
It happens something that I do not understand because the first time that CLegend is called the legend appears not centered (respect to axes) but the following callings through the listbox options it does is centered.
Other problem that occurs is that if I delete the command line
set(haxes,'Units','normalized')
then, although the legend is centered, the axes appear with a reduced size.
I also have found that the first time Clegend is calles, the position of the axes (variable posAx) is different from the following callings (in which posAx is always the same).
I have thought that when the units were changed, the position of the object was not changed but this problem arises doubts inside my mind.
In the Figure Properties documentation, it is said of Unit
This property affects the CurrentPoint and Position properties. If you
change the value of Units, it is good practice to return it to its
default value after completing your computation so as not to affect
other functions that assume Units is the default value.
Can it explain your problems?
After a lot of proofs, I have found that my problem was the resizing of the figure. The first time CLegend was called the figure window had its original size. The following calls of CLegend happened with the figure windows maximized.