Plotting Accelerometer 3 axis value in Matlab - matlab

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)

Related

MatLab: updating two separate, animated figures, visualizing weight and error

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.

Forming a curve between two lines

Can anyone let me know how should i go about creating a smooth curve in matlab
Problem statement: There are two straight line and i want to join them with a smooth curve. The dimensions of the curve are not limited to any specific dimensions. It is fine as long as there is a smooth continuity or the two lines are connected to each other by a smooth curve as shown in below figure
final Image
I hope the problem statement is clear and please let me know incase if anything is not clear.
I am using the following code and being a beginner I know its not a prefect code and there maybe mistakes. I would be glad if anyone can let me know how can i implement this curve in the form of a code in matlab.
s=10;
vec=0.6;
i=0; x=0; y=0; z=0; x1=0; y1=0; z1=0;
for i=1:s
x(i)=0;
z(i)=i;
y(i)=0;
end
angle=60;
j=0;
for j=1:s
if j<vec*s
x1(j)=0;
z1(j)=j;
y1(j)=0;
end
if j>=vec*s
x1(j)=x(j);
y1(j)=(z(j)-vec*s)*sind(angle)+y(i)*cosd(angle);
z1(j)=(z(j)-vec*s)*cosd(angle)-y(i)*sind(angle)+vec*s;
end
end
plot3(x1,y1,z1); xlabel('X axis'); ylabel('Y axis'); zlabel('Z axis');
use parametric interpolation (with parameter t):
plot3(x1,y1,z1); xlabel('X axis'); ylabel('Y axis'); zlabel('Z axis');
hold on;
n = length(x1);
t = (1:n)';
v = [x1;y1;z1]';
idx = [1:3 n-2:n]; % points you want to preserve
plot3(x1(idx),y1(idx),z1(idx),'o');
pp = interp1(t(idx,:),v(idx,:),'spline','pp');
tt = linspace(1,n,100);
X = ppval(pp, tt);
plot3(X(:,1),X(:,2),X(:,3));
grid on
and you get:

How do i produce an animated GIF in MATLAB?

I want to produce a animated gif of a solution to a partial differential equation. That is the gif should show the solution at specific time.
Currently I can only make pictures in which all times are plotted.
Below is my entire program, with figure(3) being my attempt of making a gif.
clear all;
close all;
%%%%%%%%%%%%
% For slide 27 of Diffusion 1D
% The equation to be graphed in latex form is
% u(x,t)=\frac{1}{L}+\frac{2}{L}\sum^{\infty}_{n=1}cos(\frac{n\pi x_0}{L})cos(\frac{n\pi x}{L})e^{-k(\frac{n\pi}{L})^2t}
%%%%%%%%%%%%
%define constants
%note that the constants listed in the file are arbitrary
L = 2; %length of the rod
k= 0.01; % Diffusivity, which is assume to be constant but can be a function of x
x0 = 1; %location of the inital condition i.e. f(x)=delta(x-x0)
tmax= 50; %maximum amount of time the simulation runs
nmax = 200; % maximum value for n, increase to accuracy
tgrid = 21; %The number of points to be evaluated in the time domain
xgrid = 51; %The number of points to be evaluated in the space domain
%initialize variables
u=zeros(tgrid,xgrid); %preallocate array used for storing values of the solution
t=linspace(0,tmax,tgrid);%We assume that time is evenly distributed
x=linspace(0,L,xgrid); %We assume that space is evenly distributed
%Plotting variables
figure(1);
hold on;
axis([0 L -inf inf]);
xlabel('x');
ylabel('u(x,t)');
%Calculation,
for i=1:tgrid
for j=1:xgrid
seriesSum=0;
%Calculate the fourier series up to nmax for each point u(x,t)
for n= 1:nmax
seriesSum= seriesSum + cos(n*pi*x0/L)*cos(n*pi*x(j)/L)*exp(-k*t(i)*(n*pi/L)^2);
end
%Finish calcuation for solution at a specific point
u(i,j)= 1/L+(2/L)*seriesSum;
end
%After we have calculated all points at time t, we graph it for time t
plot(x,u(i,:),'linewidth',4);
end
saveas(gcf,'PDE_sol.png')%Save figure as png in current directory
%run a second loop that does not include the initial condition to get a
%better view of the long term behaviour.
%Plotting variables
figure(2);
hold on;
axis([0 L -inf inf]);
xlabel('x');
ylabel('u(x,t)');
for i=2:tgrid
plot(x,u(i,:),'linewidth',4);
end
saveas(gcf,'PDE_sol_without_inital.png')%Save figure as png in current directory
%Create a gif verison of figure 2
figure(3);
axis([0 L -inf inf]);
xlabel('x');
ylabel('u(x,t)');
filename = 'PDE_sol.gif';
for i=2:tgrid
plot(x,u(i,:),'linewidth',4);
drawnow
frame = getframe(1);
im = frame2im(frame);
[imind,cm] = rgb2ind(im,256);
if i == 2;
imwrite(imind,cm,filename,'gif', 'Loopcount',inf);
else
imwrite(imind,cm,filename,'gif','WriteMode','append');
end
end
The output gif that I get is
which is clearly not animated.
Note: If you think there is a better place to post this question please direct me to it. As my issue is with the MATLAB programming language and not the math involved I thought this would be the best place to post my question.
The first input to getframe is the handle of the figure that you'd like to take a screenshot of. As you have it written, you are grabbing figure 1 which is actually referring to the first figure that you create that you aren't updating within your loop.
You have assigned a numeric handle of 3 to the figure that you create right before your last loop so you'll want to tell getframe to use that figure instead.
Additionally, I would create one plot object and update the XData and YData rather than continuously creating new plot objects. The issue with calling plot continuously is that it's slow AND it completely resets all of your axes settings such as x and y labels as well as x and y limits.
% Store the handle to the figure in hfig
hfig = figure(3);
% Create the initial plot object
hplot = plot(NaN, NaN, 'LineWidth', 4);
axis([0 L 0 2]);
xlabel('x');
ylabel('u(x,t)');
filename = 'PDE_sol.gif';
for i=2:tgrid
% Update the plot appearance
set(hplot, 'XData', x, 'YData', u(i,:));
drawnow
% Get a screenshot of THIS figure
frame = getframe(hfig);
im = frame2im(frame);
[imind,cm] = rgb2ind(im,256);
if i == 2;
imwrite(imind,cm,filename,'gif', 'Loopcount',inf);
else
imwrite(imind,cm,filename,'gif','WriteMode','append');
end
end

matlab: how do I do animated plot in a figure of two subplot

There is example of the web that shows how to do animated plot in a single figure.
However, I want to do two subplots in a single figure, such that they will show animation in a first subplot, and then the animation ina second subplot.
Using 'figure(1)' or 'figure (2)' and 'hold on', I can do the animation plot as follows. However, How do I call the subplot to do the similiar things?
So the effect I am looking for is: 1) figure that is opened and has two subplot. 2) plot the animated curve in the 1st subplot, then plot the animated curve in the 2nd subplot. 3) I want to go back to the 1st subplot to plot more things, and also go to 2nd subplot to plot more things.
figure(1); hold on; x = 1:1000;
y = x.^2;
%// Plot starts here
figure,hold on
%// Set x and y limits of the plot
xlim([min(x(:)) max(x(:))])
ylim([min(y(:)) max(y(:))])
%// Plot point by point
for k = 1:numel(x)
plot(x(k),y(k),'-') %// Choose your own marker here
%// MATLAB pauses for 0.001 sec before moving on to execue the next
%%// instruction and thus creating animation effect
pause(0.001);
end
Just do the subplot's in the loop:
for k = 1:numel(x)
subplot(1,2,1)
plot(x(k),y(k),'-') %// Choose your own marker here
subplot(1,2,2)
plot(x(1:k),y(1:k))
%// MATLAB pauses for 0.001 sec before moving on to execue the next
%%// instruction and thus creating animation effect
pause(0.001);
end
% Easiest way
x = rand(1, 11); y = rand(1, 11);
z = rand(1, 11); a = rand(1, 11);
figure
for i = 1 : 10
subplot(211)
plot(x(i : i+1), y(i : i+1), '.-k');
hold on; % include this if you want to show plot history
subplot(212)
plot(z(i : i+1), a(i : i+1), '.-k');
drawnow;
pause(0.1);
end
% If you don't want to call "plot" interatively
x = rand(1, 11); y = rand(1, 11);
z = rand(1, 11); a = rand(1, 11);
figure
subplot(211)
p1 = plot(NaN, NaN, 'marker', 'o');
subplot(212)
p2 = plot(NaN, NaN, 'marker', 'd');
for i = 1 : 10
set(p1, 'xdata', x(i : i+1), 'ydata', y(i : i+1));
set(p2, 'xdata', z(i : i+1), 'ydata', a(i : i+1));
drawnow;
pause(0.1);
end
First define your plot as a construct, so p1 = plot(x,y). Then you set up your loop and in the loop your write
set(p1,'YData',y);
This will update the plot p1s YData which is y. If you want to see it in an animated form just add a pause(0.1) %seconds after the set.

plot in all the subplots at the same time

I have a figure with 2 subplots.
I would like to know if it's possible (and how) to draw the same plots in all the subplots at the same time.
For example in the following plot I'd like to plot (x,y) simultaneously and then proceed separately.
fig1 = figure
subplot(2,1,1)
plot(x,y)
hold on
subplot(2,1,2)
plot(x,y)
hold on
subplot(2,1,1)
plot(x,z)
subplot(2,1,2)
plot(x,k)
You can do it with set using cell arrays as follows. See the documentation for details.
subplot(2,1,1);
h1 = plot(x,y); %// get a handle to the plot
subplot(2,1,2)
h2 = plot(x,y); %// get a handle to the plot
set([h1; h2], {'xdata'}, {x1; x2}, {'ydata'}, {y1; y2})
%// new values: x1 x2 y1 y2
If you're asking because you want to plot the same plot behind say 16 subplots, then you could do it in a loop:
for k= 1:16
s(k) = subplot(4,4,k);
plot(x,y);
hold on;
end
If you just want it for 2 subplots then there is nothing wrong with your current code