I am trying to visualize a small simulation I have to do. For the task, it is not necessary but as it quite often happens: I run into the problem and want the solution before I go on.
The simulation is an ultra-simple neural net from the computational neuroscience department with an 'Oja' algorithm. I have the value plotted as scatterplot and wanted to animate the changing weight over the loops. That for itself works fine (figure 2 or f2 handle)
Then I decided to print the difference in the weight vector from one run to another calculated as the norm. I wanted this to be plotted as a line that evolves over time (figure 1 aka f1). But though I always activate figure 2 it switches back to figure 1 and plots it there.
Of course, I searched the internet as well as stackexchange where btw I found lots of fascinating stuff about animations. Just nothing that solved the problem...
Two questions:
why?
and what do I have to change to make it work?
Thanks.
Here is the code:
function [t, w, dw]=weight(X)
w=rand(2,1)*5; %Initialization
%constants
n=1;
alpha=1;
dt=0.01;
T=5;
L=length(X);
w_old=[0; 0];
s=size(w_old)
t=0;
limit=0.001;
%handles for the figures Error and weight animation
f1= figure
set(f1,'DoubleBuffer','on', 'Name','Error');
ax1 = axes('Position',[0.1 0.1 0.7 0.7]);
f2=figure
set(f2, 'Name', 'weights');
%normalizing and plot
X=[X(:,1)-mean(X(:,1)),X(:,2)-mean(X(:,2))];
scatter(X(:,1),X(:,2));
%function handle for the error and the weights animation
herror = animatedline('Marker','.');
hLine = line('XData',w(1), 'YData',w(2), 'Color','r', ...
'Marker','o', 'MarkerSize',6, 'LineWidth',2);
hTxt = text(w(1), w(2), sprintf('(%.3f,%.3f)',w(1),w(2)), ...
'Color',[0.2 0.2 0.2], 'FontSize',8, ...
'HorizontalAlignment','left', 'VerticalAlignment','top');
while (t<T)
for i=1:L
w_old= w;
u=X(i,:);
v=u*w;
w=w+dt*n*(v*u'-alpha*v^2*w); %Oja rule
figure(f2);
hold on;
set(hLine, 'XData',w(1), 'YData',w(2))
set(hTxt, 'Position',[w(1) w(2)], ...
'String',sprintf('(%.3f,%.3f,%.2f)',[w(1) w(2) t]))
drawnow %# force refresh
%#pause(DELAY)
hold off;
dw=norm(w_old-w);
figure(f1)
hold on;
addpoints(herror, (i*t/dt),dw)
drawnow
hold off;
if dw<limit, break; end
end
t=t+dt;
if ~ishandle(hLine), break; end
end
end
You created a new axes ax1 overlapping to the plot hiding the animation.
You also put herror = animatedline('Marker','.'); after f2=figure doing nothing in the first figure before the for loop. This is the working code:
function [t, w, dw]=weight(X)
X = randn(20,2);
w=rand(2,1)*5; %Initialization
close all
%constants
n=1;
alpha=1;
dt=0.01;
T=5;
L=length(X);
w_old=[0; 0];
s=size(w_old);
t=0;
limit=0.001;
%handles for the figures Error and weight animation
f1= figure(1);
hold on;
set(f1,'DoubleBuffer','on', 'Name','Error');
%ax1 = axes('Position',[0.1 0.1 0.7 0.7]);
%function handle for the error and the weights animation
herror = animatedline('Marker','.');
xlabel('t'); ylabel('Error');
f2=figure(2);
hold on;
set(f2, 'Name', 'weights');
xlabel('W1'); ylabel('W2')
%normalizing and plot
X=[X(:,1)-mean(X(:,1)),X(:,2)-mean(X(:,2))];
scatter(X(:,1),X(:,2));
hLine = line('XData',w(1), 'YData',w(2), 'Color','r', ...
'Marker','o', 'MarkerSize',6, 'LineWidth',2);
hTxt = text(w(1), w(2), sprintf('(%.3f,%.3f)',w(1),w(2)), ...
'Color',[0.2 0.2 0.2], 'FontSize',8, ...
'HorizontalAlignment','left', 'VerticalAlignment','top');
while (t<T)
for i=1:L
w_old= w;
u=X(i,:);
v=u*w;
w=w+dt*n*(v*u'-alpha*v^2*w); %Oja rule
set(hLine, 'XData',w(1), 'YData',w(2))
set(hTxt, 'Position',[w(1) w(2)], ...
'String',sprintf('(%.3f,%.3f,%.2f)',[w(1) w(2) t]))
drawnow %# force refresh
dw=norm(w_old-w);
figure(f1);
addpoints(herror, (i*t/dt),dw)
drawnow
if dw<limit; break; end
end
t=t+dt;
if ~ishandle(hLine), break; end
end
end
To me, it seems more natural using just one window and 2 subplots instead of switching window back and forth.
Related
I'm trying to create an animated plot but my code is very slow, perhaps the method I'm using is too naive. In the below example, I have 4 subplots each with 3 lines, which I update in a 'time' loop.
clc;clear;close all;
state = {'$x-Position$','$x-Velocity$','$y-Position$','$y-Velocity$'};
ylabels = {'$x$','$\dot{x}$','$y$','$\dot{y}$'};
options1 = {'interpreter','latex'};
options2 = {'interpreter','latex','fontsize',20};
maxT = 300;
for pp = 1:4
hh1(pp)=subplot(2,2,pp);
xlabel('$t$',options2{:});
ylabel(ylabels{pp},options2{:});
title(state{pp},options1{:})
xlim([0 maxT])
hold on
end
x = randn(4,300);
z = randn(4,300);
x_est = randn(4,300);
for k = 2:maxT
for p = 1:4
plot(hh1(p),k-1:k,x(p,k-1:k),'b','linewidth',2)
plot(hh1(p),k-1:k,z(p,k-1:k),'m')
plot(hh1(p),k-1:k,x_est(p,k-1:k),':k','linewidth',2)
end
drawnow;
end
As can be seen from the profiler output, the drawnow is killing the time. Is there any way I can be more efficient in creating this animation?
Because you want an animation, there is no alternative to using drawnow to update the frame. However, it's not drawnow in particular which is slowing you down - the profiler can be misleading... drawnow simply updates all of the graphics changes since the last re-draw, which in your case is a dozen new plots!
You'll find that hold is pretty slowing. For instance if you're wiser about your holding, remove the existing hold on and only hold when actually plotting
% ... above code the same but without 'hold on'
for p = 1:4
hold(hh1(p), 'on');
% plots
hold(hh1(p), 'off');
end
This saves ~10% time on my PC (12.3sec down to 11.3sec).
The real speed up comes from removing hold entirely, along with all of the individual plot calls! This method also doesn't touch the line formatting which will help with speed. See a previous question about updating plot data here.
Simply update the plot data instead of adding plots. This gives me a speedup of ~68% (12.3sec down to 4.0sec).
% ... your same setup
% Initialise plot data
x = randn(4,300);
z = randn(4,300);
x_est = randn(4,300);
plts = cell(4,3);
hh1 = cell(4,1);
% Loop over subplots and initialise plot lines
for p = 1:4
hh1{p}=subplot(2,2,p);
xlabel('$t$',options2{:});
ylabel(ylabels{p},options2{:});
title(state{p},options1{:})
xlim([0 maxT])
% Hold on to make 3 plots. Create initial points and set line styles.
% Store the plots in a cell array for later reference.
hold on
plts{p,1} = plot(hh1{p},1:2,x(p,1:2),'b','linewidth',2);
plts{p,2} = plot(hh1{p},1:2,z(p,1:2),'m');
plts{p,3} = plot(hh1{p},1:2,x_est(p,1:2),':k','linewidth',2);
hold off
end
% March through time. No replotting required, just update XData and YData
for k = 2:maxT
for p = 1:4
set(plts{p,1}, 'XData', 1:k, 'YData', x(p,1:k) );
set(plts{p,2}, 'XData', 1:k, 'YData', z(p,1:k) );
set(plts{p,3}, 'XData', 1:k, 'YData', x_est(p,1:k) );
end
drawnow;
end
Now the plotting is pretty optimised. If you want the animation to be even quicker then just plot every 2nd, 3rd, ..., nth timestep instead of every timestep by using for k = 2:n:maxT.
I have two task to do
getting data serially from microcontroller.
plotting the 3 axis value in real time.
For first I used the following code:
s=serial('COM10');
fopen(s);
out=fscanf(s);
while(out~=0)
out=fscanf(s);
disp(out);
end
fclose(s);
now in second part i have to plot there data in real time how can i do it ,m new to matlab i tried the following sample code to plot 3 values but didn't worked out. please help.
x = -50;
y = 10;
z = 20;
while(1)
plot3(x,y,z);
XLABEL('X Axis');
YLABEL('Y Axis');
ZLABEL('Z Axis');
set(gca, 'XColor', 'r', 'YColor', [0 0.5 0.5], 'ZColor', 'y')
x=x+2;
y=y+2;
z=z+2;
end
Change the code, use plot only once:
X=[];Y=[];Z=[];
x=0;y=0;z=0;
figure(1);
myplot=plot3(x,y,z);
while(1)
x=x+1;
y=sin(x);
z=cos(x);
X(end+1)=x;
Y(end+1)=y;
Z(end+1)=z;
set(myplot,'Xdata',X,'YData',Y,'ZData',Z);
drawnow limitrate;
end
If the loop run long enough, don't forget to limit X,Y,Z sizes(for example every 1000 samples delete 500)
I'm trying to save .jpg files from MATLAB, but I don't want to have the figure pop up every time since this really slows down the process. However, setting 'visible' to 'off' doesn't seem to work. What can I do to get the Figure window to not pop up?
nFrames = 2557; % Number of frames. Number of days between 1/1/2008 and 1/31/2014
for k = 183:nFrames % 183 (7/1/2008) to the number of days. Data before this is either missing or from HI
% Map of conterminous US
ax = figure(1);
set(ax, 'visible', 'off', 'units','normalized','outerposition',[0 0 1 1]) % Make window that shows up full sized, which makes saved figure clearer
ax = usamap('conus');
states = shaperead('usastatelo', 'UseGeoCoords', true,...
'Selector',...
{#(name) ~any(strcmp(name,{'Alaska','Hawaii'})), 'Name'});
faceColors = makesymbolspec('Polygon',...
{'INDEX', [1 numel(states)], 'FaceColor', 'none'}); % NOTE - colors are random
geoshow(ax, states, 'DisplayType', 'polygon', ...
'SymbolSpec', faceColors)
framem off; gridm off; mlabel off; plabel off
hold on
% Plot data
scatterm(ax,str2double(Lat_PM25{k}), str2double(Lon_PM25{k}), 40, str2double(data_PM25{k}), 'filled'); % Plot a dot at each Lat and Lon with black outline around each dot (helps show dots that are too low for it to be colored by the color bar
% Draw outline around dot with 'MarkerEdgeColor', [0.5 0.5 0.5] for gray
hold on
% Colorbar
caxis([5 30]);
h = colorbar;
ylabel(h,'ug/m3');
% Title
% date = datenum(2007, 04, 29) + k; % Convert t into serial numbers.
title(['PM2.5 24-hr Block Average Concentration ', datestr(cell2mat(date_PM25(k)), 'mmm dd yyyy')]); % Title changes every day;
% Capture the frame
mov(k) = getframe(gcf);
% Set size of paper
set(gcf,'Units','points')
set(gcf,'PaperUnits','points')
size = get(gcf,'Position');
size = size(3:4);
set(gcf,'PaperSize',size)
set(gcf,'PaperPosition',[0,0,size(1),size(2)])
% Save as jpg (just specify other format as necessary) - Must set 'facecolor' to 'none' or else color of states turn out black
eval(['print -djpeg map_US_' datestr(cell2mat(date_PM25(k)),'yyyy_mm_dd') '_PM25_24hrBlkAvg.jpg']);
clf
end
The problem is with getframe. A detailed answer was given in this thread and in this discussion.
In short you need to avoid getframe and instead do something like the following:
ax = figure(1);
set(ax, 'visible', 'off')
set(ax, 'PaperPositionMode','auto')
aviobj = avifile('file.avi');
....
for k=1:N
%# all the plotting you did before the getframe
img = hardcopy(ax, '-dzbuffer', '-r0');
aviobj = addframe(aviobj, im2frame(img));
end
aviobj = close(aviobj);
I have used the code provided by #Amro in other question:
%# control animation speed
DELAY = 0.01;
numPoints = 600;
%# create data
x = linspace(0,10,numPoints);
y = log(x);
%# plot graph
figure('DoubleBuffer','on') %# no flickering
plot(x,y, 'LineWidth',2), grid on
xlabel('x'), ylabel('y'), title('y = log(x)')
%# create moving point + coords text
hLine = line('XData',x(1), 'YData',y(1), 'Color','r', ...
'Marker','o', 'MarkerSize',6, 'LineWidth',2);
hTxt = text(x(1), y(1), sprintf('(%.3f,%.3f)',x(1),y(1)), ...
'Color',[0.2 0.2 0.2], 'FontSize',8, ...
'HorizontalAlignment','left', 'VerticalAlignment','top');
%# infinite loop
i = 1; %# index
while true
%# update point & text
set(hLine, 'XData',x(i), 'YData',y(i))
set(hTxt, 'Position',[x(i) y(i)], ...
'String',sprintf('(%.3f,%.3f)',[x(i) y(i)]))
drawnow %# force refresh
%#pause(DELAY) %# slow down animation
i = rem(i+1,numPoints)+1; %# circular increment
if ~ishandle(hLine), break; end %# in case you close the figure
end
but I need to change the velocity of the marker. I've tried changing the value of DELAY, but it didn't work. The point is that I can't change the numPoints (size of the function), so I don't know hot to do it.
Any ideas?
Thanks!
Just uncomment the pause(DELAY) in the infinite loop. Change DELAY to a suitable value
I am looking to create a simple log(x) graph within MATLAB in which the model shows the point moving along the curve with time.
The overall aim is to have two of these graphs alongside one another and to apply an algorithm to them. I am really unsure where to start here.
I am relatively new at MATLAB coding so any help would be very useful!
Thanks
Luke
Here is a variation on #Jacob's solution. Instead of redrawing everything at each frame (clf) we simply update the point's location:
%# control animation speed
DELAY = 0.01;
numPoints = 600;
%# create data
x = linspace(0,10,numPoints);
y = log(x);
%# plot graph
figure('DoubleBuffer','on') %# no flickering
plot(x,y, 'LineWidth',2), grid on
xlabel('x'), ylabel('y'), title('y = log(x)')
%# create moving point + coords text
hLine = line('XData',x(1), 'YData',y(1), 'Color','r', ...
'Marker','o', 'MarkerSize',6, 'LineWidth',2);
hTxt = text(x(1), y(1), sprintf('(%.3f,%.3f)',x(1),y(1)), ...
'Color',[0.2 0.2 0.2], 'FontSize',8, ...
'HorizontalAlignment','left', 'VerticalAlignment','top');
%# infinite loop
i = 1; %# index
while true
%# update point & text
set(hLine, 'XData',x(i), 'YData',y(i))
set(hTxt, 'Position',[x(i) y(i)], ...
'String',sprintf('(%.3f,%.3f)',[x(i) y(i)]))
drawnow %# force refresh
%#pause(DELAY) %# slow down animation
i = rem(i+1,numPoints)+1; %# circular increment
if ~ishandle(hLine), break; end %# in case you close the figure
end
A simple solution is:
x = 1:100;
y = log(x);
DELAY = 0.05;
for i = 1:numel(x)
clf;
plot(x,y);
hold on;
plot(x(i),y(i),'r*');
pause(DELAY);
end
You may want to have a look at the COMET function, which will make an animation of the curve.
For example (using the same numbers as #Jacob)
x = 1:100;
y = log(x);
comet(x,y)
If you want to show the point moving on the line (not 'drawing' it), you simply plot the line before
x = 1:100;
y = log(x);
plot(x,y,'r')
hold on %# to keep the previous plot
comet(x,y,0) %# 0 hides the green tail
a little more complex solution along the same lines as #Jacob. Here I add some optimization using handle graphics and a MATLAB movie object for playback.
x=1:100;
y=log(x);
figure
plot(x,y);
hold on; % hold on so that the figure is not cleared
h=plot(x(1),y(1),'r*'); % plot the first point
DELAY=.05;
for i=1:length(x)
set(h,'xdata',x(i),'ydata',y(i)); % move the point using set
% to change the cooridinates.
M(i)=getframe(gcf);
pause(DELAY)
end
%% Play the movie back
% create figure and axes for playback
figure
hh=axes;
set(hh,'units','normalized','pos',[0 0 1 1]);
axis off
movie(M) % play the movie created in the first part
solution can be this way
x = .01:.01:3;
comet(x,log(x))