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
Related
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.
I'm having an issue where, for the first for loop I'm getting my desired output at no2_iterate(1) but within the second loop made to generate no2_iterate(2) I'm not getting any output whatsoever.
The following are my two if-statements/for loops to generate no2_iterate(1) & (2).
no2_sum_1cm = 0;
gridh_iterate = 0 % starting height in cm
lato = 1;
lono = 1;
no2_iterate_start = 0;
no2_iterate(1:2) = 0;
if gridh_iterate < gridh(lato,lono,1);
no2_layer = no2_moleccm3(lato,lono,1,12);
for i = 1:gridh(lato,lono,1);
for h = 1;
gridh_iterate = gridh_iterate+ 1; % in cm, now compare to gridh(1,1,1) and the other areas, so if its over the height of the cell switch no2 concn
end
no2_iterate(1) = (no2_iterate(1) + no2_layer)*1; % Now units of g no2/cm2
end
no2_iterate = no2_iterate
end
if gridh_iterate < gridh(lato,lono,2) && gridh_iterate >gridh(lato,lono,1);
no2_layer = no2_moleccm3(lato,lono,2,12);
for i = 1:gridh(lato,lono,2);
for h = 1;
gridh_iterate = gridh_iterate + 1; % in cm, now compare to gridh(1,1,1) and the other areas, so if its over the height of the cell switch no2 concn
end
no2_iterate(2) = (no2_iterate(2) + no2_layer)*1; % Now units of g no2/cm2
end
no2_iterate = no2_iterate;
end
I suspect that my issue is within the second if-statement where I specify that I want the range to be between two separate variables that I am somehow excluding all variables.
I ended up figuring out the problem. Here is the code that solved my troubles!
Also thank you to commentators for helping me clear some of the noise from my code that didn't do anything practical.
no2_sum_1cm = 0;
gridh_iterate = 0 % starting height in cm
lato = 1;
lono = 1;
no2_iterate_start = 0;
no2_iterate(1:27) = 0;
if gridh_iterate <= gridh(lato,lono,1);
no2_layer = no2_moleccm3(lato,lono,1,12);
for i = 1:gridh(lato,lono,1);
gridh_iterate = gridh_iterate+ 1; % in cm, now compare to gridh(1,1,1) and the other areas, so if its over the height of the cell switch no2 concn
no2_iterate(1) = (no2_iterate(1) + no2_layer)*1; % Now units of g no2/cm2
end
end
gridh_iterate = 0;
if gridh_iterate <= gridh(lato,lono,2) %&& gridh_iterate>gridh(lato,lono,1);
no2_layer = no2_moleccm3(lato,lono,2,12);
for i = 1:gridh(lato,lono,2);
gridh_iterate = gridh_iterate + 1; % in cm, now compare to gridh(1,1,1) and the other areas, so if its over the height of the cell switch no2 concn
no2_iterate(2) = (no2_iterate(2) + no2_layer)*1; % Now units of g no2/cm2
end
end
I ended up realizing since "gridh" variables specified the height of a single cell and not total heights that it was irrelevant to list a range of height data to stay between and that for my purposes I should just set the height of a grid cell I was interesting in as the max, so that my code would iterate through it and then cease.
I have a script that I'm running, and at one point I have a loop over n objects, where I want n to be fairly large.
I have access to a server, so I put in a parfor loop. However, this is incredibly slow compared with a standard for loops.
For example, running a certain configuration ( the one below ) with the parfor loop on 35 workers took 68 seconds, whereas the for loop took 2.3 seconds.
I know there's stuff to do with array-broadcasting that can cause issues, but I don't know a lot about this.
n = 20;
r = 1/30;
tic
X = rand([2,n-1]);
X = [X,[0.5;0.5]];
D = sq_distance(X,X);
A = sparse((D < r) - eye(n));
% Infected set I
I = n;
[S,C] = graphconncomp(A);
compnum = C(I);
I_new = find(C == compnum);
I = I_new;
figure%('visible','off')
gplot(A,X')
hold on
plot(X(1,I),X(2,I),'r.')
hold off
title('time = 0')
axis([0,1,0,1])
time = 0;
t_max = 10; t_int = 1/100;
TIME = 1; T_plot = t_int^(-1) /100;
loops = t_max / T_plot;
F(loops) = struct('cdata',[],'colormap',[]);
F(1) = getframe;
% Probability of healing in interval of length t_int
heal_rate = 1/3; % (higher number is faster heal)
p_heal = t_int * heal_rate;
numhealed = 0;
while time < t_max
time = time+t_int;
steps = poissrnd(t_int,[n,1]);
parfor k = 1:n
for s = 1:steps(k)
unit_vec = unif_unitvector;
X_new = X(:,k) + unit_vec*t_int;
if ( X_new < 1 == ones(2,1) ) ...
& ( X_new > 0 == ones(2,1) )
X(:,k) = X_new;
end
end
end
D = sq_distance(X,X);
A = sparse((D < r) - eye(n));
[S,C] = graphconncomp(A);
particles_healed = binornd(ones(length(I),1),p_heal);
still_infected = find(particles_healed == 0);
I = I(still_infected);
numhealed = numhealed + sum(particles_healed);
I_new = I;
% compnum = zeros(length(I),1);
for i = 1:length(I)
compnum = C(I(i));
I_new = union(I_new,find(C == compnum));
end
I = I_new;
if time >= T_plot*TIME
gplot(A,X')
hold on
plot(X(1,I),X(2,I),'r.')
hold off
title(sprintf('time = %1g',time))
axis([0,1,0,1])
% fprintf('number healed = %1g\n',numhealed)
numhealed = 0;
F(TIME) = getframe;
TIME = TIME + 1;
end
end
toc
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
I am trying to use the PDE heat equation and apply it to images using Matlab. The problem i am having is that the image isn't blurring , it is just going white. Also, I am getting different results from the rest of the class who is using Maple.
Here is the code:
% George Lees Jr.
% Heat equation
clear,clc;
dx = 1;
dy = 1;
dt = .025;
%dt/(dx*dx)
t = 0;
time = 3;
T_old = imread('tulipgray.jpg');
T_temp=T_old;
[m,n,k] = size(T_temp);
%colormap gray;
%imagesc(T_temp);
%imshow(T_old);
T_new = T_temp;
T_new=ind2gray(T_new,colormap);
%T_new(:,50)=0;
%T_old(1,70)
%imagesc(T_new);
%diff_x = dt/(dx*dx)
%diff_y = dt/ (dy*dy)
%time = 0;
while t < time
for i = 2:1:m-1
for j = 2:1:n-1
T_new(i,j) = T_temp(i,j) + dt*(T_temp(i+1,j) -2*T_temp(i,j) + T_temp(i-1,j)) + dt*(T_temp(i,j+1)-2*T_temp(i,j) + T_temp(i,j-1));
t = t+dt;
T_temp(i,j) = T_new(i,j);
end
end
end
figure
imshow(T_new)
Yeah the image just gets whiter
There's 2 issues with your code:
1) you're incrementing the time counter after each individual pixel instead of after doing the whole image
2) you need to do the calculations on floating points values, not integers. dt is small, so the values from the RHS of the equation are <1
Fixed code should look something like this
clear,clc;
dt = 0.025;
time = 3;
T_old = imread('rice.png');
T_temp=double(T_old);
[m,n,k] = size(T_temp);
T_new = double(T_temp);
T_new=ind2gray(T_new,colormap);
while t < time
for i = 2:1:m-1
for j = 2:1:n-1
T_new(i,j) = T_temp(i,j) + dt*(T_temp(i+1,j) -2*T_temp(i,j) + T_temp(i-1,j)) + dt*(T_temp(i,j+1)-2*T_temp(i,j) + T_temp(i,j-1));
end
end
T_temp = T_new;
t = t+dt;
imshow(uint8(T_new))
getframe;
end