plotting 2 variable of different size in matlab - matlab

Am trying to plot 2 variable of different size length in matlab GUI using push button,
but because the variables are of different length it will not work,is there a way i can make it to plot.
d= pdist([x,y,z],'euclidean') ; % value of my distance
dd= 1:10:d; % interval and end 'd' value
FSL=-120; %value of free space loss get from the GUI
DFSL= 1:10:FSL %interval and end at FSL value
plot(dd,DFSL)
The plot code didnt work coming back with an error "
Error using plot
Vectors must be the same lengths"

You can plot vectors of two different lengths, but not against each other. You have used the syntax
plot(x,y)
which means for every element in vector x, there should be a corresponding element in vector y. In your case, you do not have this, hence the error.
You can plot like this though:
plot(x)
figure;
plot(y)
If you are looking to plot them in a single plot, subplot will be useful.

Related

Animated Discrete Stem Plot

So I want to create an animated plot of a discrete-time complex exponential function. The simplest non-animated plot would be given by this:
n=-5:40;
x=(exp((3*4j)*n)).*(n>=0);
y=real(x);
subplot(2,1,1);
stem (n,y)
z=imag(x);
subplot(2,1,2);
stem (n,z)
How do I animate it to show the function for the different numbers of samples considered in a given interval (assuming I have a time interval specified by start second and end second and a vector containing the number of sample values in the given interval)?
I tried along these lines:
figure,hold on
xlim([min(x(:)) max(x(:))])
ylim([min(y(:)) max(y(:))])
%// Plot point by point
for k = 1:numel(x)
stem (k,y) %// Choose your own marker here
pause(0.001);
end
That doesn't compile. How to achieve this?
Short answer:
Make the following two changes:
① Replace xlim([min(x(:)) max(x(:))]) with this xlim([1 numel(x)]).
② Replace stem (k,y) with this: stem (k,y(k)).
Detailed Answer:
① xlim([min(x(:)) max(x(:))]) is giving you the following error:
Error using matlab.graphics.axis.Axes/set
While setting the 'XLim' property of 'Axes':
This is not a valid LimitsWithInfs value. Complex inputs are not supported
The error message tells you exactly what the problem is. Your vector x contains complex numbers. Time axis having complex numbers also doesn't imply anything.
It seems that you would want to replace this line xlim([min(x(:)) max(x(:))]) with this: xlim([1 numel(x)]).
② Inside the loop, stem (k,y) is giving you this error:
Error using stem (line 46)
X must be same length as Y.
The error message tells you exactly what the problem is. Here k is just a scalar (1x1) but y is a 1x46 vector. Since you want to plot y point by point, replace stem (k,y) with this: stem (k,y(k)).
Output after making the mentioned fixes:

Matlab, figures and for loops

I am trying to plot the following simple function; $y=A.*x$ with different A parameter values i.e. A=0,1,2,3 all on the same figure. I know how to plot simple functions i.e. $y=x$ by setting up x as a linspace vector so defining x=linspace(0,10,100); and I know that one can use the hold command.
I thought that one could simply use a for loop, but the problem then is getting a plot of all the permutations on one figure, i.e. I want a plot of y=t,2*t,3*t,4*t on the same figure. My attempt is as follows:
x=linspace(0,10,100);
%Simple example
Y=x;
figure;
plot(Y);
%Extension
B=3;
F=B*x;
figure;
plot(F);
%Attempt a for loop
for A= [0,1,2,3]
G=A*x;
end
figure;
plot(G);
This is how I would plot your for loop example:
figure;
hold all;
for A=[0,1,2,3]
G=A*x;
plot(G);
end
figure creates a new figure. hold all means that subsequent plots will appear on the same figure (hold all will use different colours for each plot as opposed to hold on). Then we plot each iteration of G within the loop.
You can also do it without the loop. As with most things in Matlab, removing the loop should give improved performance.
figure;
A=[0,1,2,3];
G=x'*A;
plot(G);
G is the outer product of the two vectors x and A (with x having been transposed into a column vector). plot is used to plot the columns of the 100x4 matrix G.

Plotting 3 vectors in Matlab GUI axes handle

I am trying to plot 3 vectors onto matlab GUI in a serial object's callback.
I want to plot this on axes handle but the problem is it only plot last vector;
plot(handles.axes1,sensor1,'r');
plot(handles.axes1,sensor2,'b');
plot(handles.axes1,sensor3,'g');
I searched on internet and find that this issue can be solved with hold on and hold of feature so I tried this
plot(handles.axes1,sensor1,'r');
hold on ;
plot(handles.axes1,sensor2,'b');
plot(handles.axes1,sensor3,'g');
hold off;
but in this case a new figure is opened(dont know why) and again only the last plot is drawn.
I am stucked. If any one have idea of what would be the issue?
Thanks
I'm not sure why your first try using "hold" didn't work. Seems like it should have.
But in any case, you can get the desired behavior in a single command:
plot(handles.axes1,length(sensor1),sensor1,'r',...
length(sensor2),sensor2,'b',...
length(sensor3),sensor3,'g');
This specifies both an X = length(sensor_) and a Y = sensor_ to the plot command. When you only give plot a Y input, it assumes an X of length(Y). But you can't combine multiple traces in a single plot command by giving only the Y input for each, because it will try to treat the inputs as X,Y pairs.
As the vectors are the same length we can simply combine them as the columns of a matrix and then plot the matrix
plot(handles.axes1,[sensor1',sensor2',sensor3'])
However these will have the default colour order. Without specifying x values setting colors within the plot command is tricky. However (luckily) the default order starts:
blue,green,red...
so swapping the column order will plot the lines with the colours requested
plot(handles.axes1,[sensor2',sensor3',sensor1'])
(this assumes the vectors are rows, if they are columns don't transpose them)

MATLAB: Plotting different numerical approximation in one plot

I have the following matlab code for approximating a differential equation via the Euler-method:
% Eulermethod
a=0;
b=0.6;
Steps=6;
dt=(b-a)/Steps;
x=zeros(Steps+1,1);
x(1,1)=1;
y=zeros(Steps+1,1);
for i=1:Steps
x(i+1,1)=x(i,1)+dt*(x(i,1)*x(i,1)+1);
end
plot(x)
I want to be able to plot the solution plot for several different values of Steps in one plot and have the x-axis go from 0 to 0.6 instead of from for example 1 to 100 000 etc. Can this be done?
If you use the hold on command this will allow you achieve multiple plots on the same figure. Similarly, if you separate your data into x and y vectors, you can plot them against eachother by passing 2 vectors to plot instead of just one. For example
figure
hold on
for i=1:m
x = [];
y = [];
%% code to populate your vectors
plot(x,y)
end
You should now see all your plots simultanesously on the same figure. If you want x to be composed of n equally spaced elements between 0 and 0.6, you could use the linspace command:
x = linspace(0.0,0.6,n);
In order to distinguish your plots, you can pass an extra paramter to the function .For example
plot(x,y,'r+')
will plot the data as a series of red + symbols.
Plot can take more arguments: plot(x_axis,values, modifiers);
If x-axis is a vector of M elements, values can be a matrix of MxN elements, each of which are drawn with a separate color.

Is there any way to get all points which was drawin on matlab plot

Hi
I was wondering if there is any way to get all points which was drawn on Matlab plot. Let's say that I drawn one line on plot and to draw this line I used just two points - matlab ploter connected these points and I get a line. Is there any way to get all the points which are on that line, without saving this plot to file ??
If you plot a line from two points, e.g. plot([x1 x2],[y1 y2]), the easiest way to get all the plots on the line is to calculate them directly.
nPts = 100; %# number of points on the line you want
%# listOfPoints is a 2-by-nPts array with all the points on the line
listOfPoints = [x1:(x2-x1)/(nPts-1):x2;y1:(y2-y1)/(nPts-1):y2];
You can generate the points you are looking for through basic linear regression. Feed in your x and y variables to Matlab's regression function and it calculates the coefficients of the line of plot(x,y). With the line equation set up, you can feed in a list of new x variables and it will calculate the corresponding y values.
x=[x1; x2];
y=[y1; y2];
b = regress(y,[ones(length(x),1) x])
new_y=b(1)+b(2)*[new_x1:new_x2]