Matlab making a simple plot - matlab

I have an array a
I checked its status and got
>>size(a)
354 2
>>class(a)
double
I tried to do
>>plot(a)
but i got two graphs and not one

plot(a) creates a 2-D line plot of the data in a versus the index of each value.
Since there is size of 354,2, it creates 2 plots.. Look at this example: http://www.mathworks.com/help/matlab/ref/plot.html#btzptin
In the same example, there was a statement:
plot(X,Y) creates a 2-D line plot of the data in Y versus the corresponding values in X.
So you need to find x vector which forms the x axis and Y in your case will be a or depending on what is in a. If it contains both x and y, then you plot(a(:,1),a(:,2)).

Related

Limits of the Waterfall function ... or not?

I have a problem to figure out how in a waterfall figure the x axis can correspond to the x values, and not their point number. This question seems rather simple but in my particular case (due to the size of vectors) it's not easy to get the correct figure. So i really need your help ... after several hours of unsatisfied results.
Assuming that two vectors x and y of the same length are recorded at a time t. This procedure is performed k times. I finally want to plot with waterfall y versus x for the different times.
I give you a script that corresponds to the experiment where xx is just added to get here two continuous functions x and y for the different times. The result is almost perfect but I would like the x-y values on the corresponding x, y axis instead of the point number.
xx=0:0.1:8;
for t=1:2:11
x(t,:)=sin(t*xx.^2);
y(t,:)=cos(t*xx.*4);
end
waterfall(x,y)
The problem comes probably from the different size of x, y with t. Thanks in advance for your advice.
Two comments:
waterfall takes either Z or X,Y,Z as coordinates. So it takes your x matrix as Z, and the other argument is mapped to the C input, which dictates color. You can see that the plot is the same if you do waterfall(x), except with different colors.
Your x is not monotonically increasing, so if you plot x(t,:) vs y(t,:) for any t, you'll get a web-like graph, not anything nice to look at.
So I'll plot xx vs y, and I'm modifying your y a bit so it looks nicer. I hope you can take this idea and modify it to do what you need.
The code below doesn't use waterfall at all, it simply calls plot3 once for each t. It might be possible to call plot3 with your full x and y matrices, but this is just as easy.
In the plot3 call, the x-coordinates are given by xx, the y-coordinates by t (simply repeated to match the expected size), and the z-coordinates by y:
xx = 0:0.1:8;
for t = 1:2:11
y = cos(t*xx/4);
plot3(xx,repmat(t,size(xx)),y)
hold on
end
xlabel('x')
ylabel('t')
zlabel('y=cos(tx/4)')

How to create a 2D-matrix out of my data for surf()?

I have a 25000x3-matrix, with each row containing a x-, a y- and a z-value. Now I wanted to do a graphical plot out of these. But for using for example surf(Z) I have to use a mxn-matrix as Z with m equal the size of x and n equal the size of y. How can I reshape the matrix I have to the needed mxn-matrix? The problem is that my x- and y-values are no ints, but floats, so I assume that I have to do a interpolation first. Is that true? My data plotted with plot3 looks like:
The fact that your x- and y- values are not integers is not a problem at all. The real question is: are your (x,y) points forming a grid, or not ?
If your points are forming a grid, then you have to reshape your columns to form m-by-n arrays. You may need to sort your data according to the first, then second column and then use the reshape function.
If your points are not forming a grid, then you will have to make an interpolation. By chance the scatterinterpolant class can nicely help you in doing so.
As you can see, the data you are providing is neither given in a gridded way, nor is the point cloud clean. You could however try to do the following:
Project the point cloud onto the x-y plane
Triangulate those points
Give the points their original z-coordinate back.
Plot the surface using trisurf
Here is a MATLAB code that does this:
%// Generate some points P
[X,Y] = ndgrid(0:30);
P = [X(:), Y(:), X(:).^2+Y(:)];
%%// Here the actual computation starts
[~,I] = unique(P(:,1:2),'rows'); %// Remove points with duplicate (x,y)-coords
P = P(I,:);
T = delaunay(P(:,1),P(:,2)); %// Triangulate the 2D-projection
surface = triangulation(T, P(:,1), P(:,2), P(:,3)); %// Project back to 3D
trisurf(surface); %// Plot
You may want to remove stray points first, though.

plotting 2 variable of different size in 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.

I want to plot a function in 3d in matlab

I have a function, X. It has inputs t and d.
X generates a 3x1 array, effectively an X,Y,Z tuple representing a point in 3-dimensional space.
I want to generate a plot for all values of t and d, given that 0 < t < 360 and 0 < d < 5. (They are not vectors.)
Currently I'm doing:
plot3(X(1),X(2),X(3),'.');
grid on
Which gives me just the one point in space. How should I go about doing this?
plot3(X(1),X(2),X(3),'.') is simply plotting the point generated by one pair of values for t and d.
You will want to generate an 3xN matrix for all the values of t and d that you want, perhaps using the linspace function to generate all the values of t and d that you want to plot.
Another option would be to use the ezsurf function as follows:
ezsurf('t+d',[0 360 0 5])

How can I ask matlab to give me the value of y if I input the value of x?

I already have my xy graph using the line graph. What troubles me is how can I ask matlab to give me the value of y if I give the value of x. That is, the corresponding value of y when I give x in the line I have in the graph.
What I think you want to do is interpolation.
Say your x and y values that you used for plotting are stored in xData and yData, respectively.
Then, you find a value y that corresponds to a value x using INTERP1
y = interp1(xData,yData,x);
By default, interp1 interpolates linearly, that is, it returns the values as if the dots in the plot were connected by straight lines. If you want a smoother interpolation, you'd use
y = interp1(xData,yData,x,'cubic');