How do I draw 2D Map with plot() in MATLAB - matlab

I am trying to create a 2D map of some place. I get a 181x1 vector of laser sensor readings from a robot. All the values in this vector corresponds to a distance from that single angle like 1°,2°..180°. The problem here is I need to create a map by plotting these distances as dots with plot() or a similar function to it.

there is a function for plotting in polar coordinates. try
>> polar( (0:180)/180*pi, distanceVector)

You can convert your angle-distance coordinates to Cartesian X and Y with POL2CART function.
[X,Y] = pol2cart((1:180)/180*pi, distanceVector);
Then you can use PLOT.
plot(X,Y,'.')

plot(theVector, '.')
if you need to plot as dots instead of lines. If the dot is too small, try to plot as circles.
plot(theVector, 'o')
See http://www.mathworks.com/access/helpdesk/help/techdoc/ref/linespec.html for detail.

Related

matlab 4D interpolation plot

Here is a sample dataset.
x = linspace(10,20,50);
y = linspace(10,20,50);
z = cos(linspace(0,2*pi,50));
time = linspace(1,60,50);
sci_temp = randi(100,50,1);
x,y,z is position, and sci_temp is temperature data.
I wonder how to draw trajectory over xyz position with sci_temp data.
I try interpolate data, so I get 2D interpolate time vs z with sci_temp plot.
S = TriScatteredInterp(time',z',sci_temp);
[t_mesh z_mesh] = meshgrid(time,z);
tz_mesh = S(t_mesh, z_mesh);
pcolor(t_mesh,z_mesh,tz_mesh)
What I want to get is a 2D (time vs sci_temp) section map on a xyz 3D plot. Like this image.
how to show trajectory over sci_temp in 3D plot?
Is there anyone can help me?
Thanks
First, you are doing your interpolation slightly wrong, you don't want to interpolate over the mesh created by
meshgrid(time,z);
Because it will duplicate values. You want to interpolate over the mesh created by
meshgrid(time,linspace(min(z),max(z),50));
Once you get the interpolated values like:
You can plot them in 3D space with the surface function, you just need to make sure to give x and y coordinates appropriately
surface(repmat(x,50,1),repmat(y,50,1),zmesh,tzmesh)
You can even have more complex paths, for example, same data but y=z
Edit: Also I forgot to mention, I'd suggest you to use scatteredInterpolant instead of TriScatteredInterp. It gives you more control over the interpolation

Drawing a set of arrows in matlab

I have a series of "from" and "to" coordinates in 2D and I would like to draw in matlab a bunch of arrows that begin and end in those coordinates. How do I do that?
I tried champ but that didn't work.
You can draw a single arrox using the annotation function:
h=annotation('arrow',[0 1],[0 1])
for a set of arrows, just loop
You can probably use the quiver plot.
quiver(x,y,u,v)
Plots arrows starting at (x,y) with the direction and length of (u,v). x through v can be vectors of the same length. One arrow is plotted for each of the elements.
Of course you first have to compute the displacement vector (u,v) from your start and endpoints.

Plotting a 2D vector field with a 3D matrix quiver -MATLAB

I am having real trouble with vectors/matrices etc. in matlab.
I have a 3D vector nxmx2 where n is my x-coordinate, m is my y-coordinate and for each (x,y) there are two z values, the first being my velocity in the x-direction, and the second being my velocity in the y-direction.
How do I plot these velocity functions on a x-y graph? I have been trying with things like:
quiver(A)
quiver(A,A,A(:,:,1),A(:,:,2))
But can't seem to get it to work.
The correct way to use quiver for the input you describe is
quiver(A(:,:,1), A(:,:,2));
If you want the axes of the plot to have equal-spaced values different than 1,2,... you should use meshgrid to generate them, as shown in the examples in the quiver documentation.

How to plot 4D contour lines (XYZ-V) in MATLAB?

I have dataset of XYZ as the coordinates and V as the value at each point (100x4 matrix).
I plot the 3D surface using patch. (by faces & vertices)
How can I plot the contour lines of V (NOT Z) over the 3D surface !?
( The Contour3 function plots 3D contour lines of Z ; But I need contour lines of V. )
Actually I want something like this or this.
Thanks a billion for your help.
Well actually I found out that the isosurface command is exactly what I want.
However, this command requires the V data to be a 3D matrix. But my V is a vector. And the data in it is completely non-uniform and irregular. Now here rises a new question :
How can I convert this non-uniform vector to a 3D matrix, so that it's ready to be used with isosurface command !!?
Please help me with this.
cont3d from MathWorks FileExchange is not exactly what you're looking for, but it may give you some ideas.

isosurface function MATLAB usage

Hi can any one give me a simple example of how to use the isosurface function in MATLAB.
The example given if you type help isosurface is quite confusing. Searching on google did not help as no one gives simple examples anywhere. All of them use predefined functions like
flow.
For starters, suppose i have points (x,y,z) where z=0 and at each point I define a constant
function f(x,y,z)=6. So if I use the isosurface function on the isovalue 6 I would like MATLAB to give me a 3d plot with the XY plane highlighted in some colour, say green.
I don't quite understand your example, but here's how you use isosurface to draw a sphere:
%# create coordinates
[xx,yy,zz] = meshgrid(-15:15,-15:15,-15:15);
%# calculate distance from center of the cube
rr = sqrt(xx.^2 + yy.^2 + zz.^2);
%# create the isosurface by thresholding at a iso-value of 10
isosurface(xx,yy,zz,rr,10);
%# make sure it will look like a sphere
axis equal
The example you gave is very uninteresting, in fact maybe even problematic.
By collapsing all points to z=0, you no longer can/need to use ISOSURFACE, and CONTOUR should be called instead. Even then, a constant function f(X,Y)=6 won't show anything either...
Since #Jonas already showed how to use ISOSURFACE, here is an example for the CONTOUR function:
%# create a function to apply to all X/Y coordinates
[X,Y] = meshgrid(-2:0.1:2,-1:0.1:1);
f = #(X,Y) X.^3 -2*Y.^2 -3*X;
%# plot the function surface
subplot(121), surfc(X,Y,f(X,Y))
axis equal, daspect([1 1 3])
%# plot the iso-contour corresponding to where f=-1
subplot(122), contour(X,Y,f(X,Y),[-1 -1]),
axis square, title('Contour where f(X,Y)=-1')