matlab 4D interpolation plot - matlab

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

Related

How to place a geographical map underneath a surf plot in Matlab?

If there is an easier way to do this in general any suggestions would be appreciated.
I have imported a .nc file into Matlab and currently have a surface plot created from the interpolated matrix of data which corresponds to longitudes and latitudes. An alpha scale has been applied to add opacity and now I would like to place a map of said longs and lats beneath it so that it acts as an overlay.
here is the code used to plot where xq,yq is the mesh longitude and latitude grid and newMatrix is the interpolated version of the original matrix. Attached is the top view surf plot which I would like a map of long lat restricted x,y axis beneath (line style or topographic)
f = figure;
ax = axes('Parent',f);
h = surf(xq,yq,newMatrix,'Parent',ax);
set(h, 'edgecolor','none');
view(ax,[0,90]);
alpha color
alpha scaled
grid off
colorbar;
I'm aware of geomap but I'm not sure how it can be used in this way without causing the axis to misalign or just not be applied correctly. Before I was using a contour plot but found it easier to interpolate and plot with surf as per this post: Matlab how to make smooth contour plot?

Graph different 2D ellipses in 3D axes at different heights in MATLAB

I want to graph different ellipses at different heights (z-coordinates).
My idea was to write the following code:
z=0:1/64:3/8;
t=linspace(-pi,pi,25);
[t,z]=meshgrid(t,z);
x=cos(-t);
y=cos(-t-4*pi*z);
I would like MATLAB to read my code like:
"Find x and y, and plot at the corresponding height (z). By doing so, join the points such that you'll form an ellipse at constant z".
I'm not sure what kind of function I could use here to do this and was hoping for someone to tell me if there exists such a function that will do the job or something similar.
In case you're wondering, I want to graph the polarization of light given two counterpropagating beams.
EDIT: While this is similar to the question draw ellipse and ellipsoid in MATLAB, that question doesn't address plotting 2D ellipses in 3D axes, which is what I am trying to do.
This can be solved by removing the meshgrid, and just using a plain old for-loop.
t = linspace(-pi,pi,25);
z = 0:1/64:3/8
f = figure;
hold on;
for i = 1:length(z)
x=cos(-t); y=cos(-t-4*pi*z(i));
plot3(x,y,z(i)*ones(length(z),1));
end
The problem in the original code is that you're trying build the ellipses all at once, but each ellipse only depends on a single z value, not the entire array of z values.
When I run this code, it produces the following plot:

3D scatter plot with 4D data

I need to plot a 3D figure with each data point colored with the value of a 4th variable using a colormap. Lets say I have 4 variables X,Y,Z and W where W = f(X,Y,Z). I want a 3D plot with X, Y, and Z as the three axis. The statement scatter3(X,Y,Z,'filled','b') gives me a scatter plot in 3D but I want to incorporate the value of Z in the graph by representing the points as an extra parameter (either with different areas :bigger circles for data points with high value of Z and small circles for data points with low value of Z or by plotting the data points with different colors using a colormap). However, I am a novice in MATLAB and dont really know how to proceed. Any help will be highly appreciated.
Thanks in advance!
So just use z for the size vector (4th input) as well as the color vector (5th input):
z = 10*(1:pi/50:10*pi);
y = z.*sin(z/10);
x = z.*cos(z/10);
figure(1)
scatter3(x,y,z,z,z)
view(45,10)
colorbar
The size vector needs to be greater 0, so you may need to adjust your z accordingly.
You are already nearly there... simply use
scatter3(X,Y,Z,s,W);
where s is the point size (scalar, e.g. 3) and W is a vector with your W values.
You might also want to issue an
set(gcf, 'Renderer','OpenGL');
where gcf gets your current figure you are plotting in to significantly increase responsiveness when scattering a lot of data.

Matlab 3d plot of indexed data

I am trying to plot a 3d view of a very large CT dataset. My data is in a 3d matrix of 2000x2000x1000 dimension. The object is surrounded by air, which is set to NaN in my matrix.
I would like to be able to see the greyscale value of the surface of the object (no isosurface) but I cannot quite work out how to do that in Matlab. Can anyone help me please?
Given that I a dealing with a huge matrix and I am only interested in the surface of the object, does anyone know a good trick how to reduce the size of my dataset?
The function surf(X,Y,Z) allows you to plot 3d data, where (X,Y) gives the coordinates in the x-y-plane while Z gives the z-coordinate and the surface color.
By default the function does not plot anything for the NaN entries, so you should be good to go with the surf function.
To set the surf-function to use a grayscale plotting use:
surf(matrix3d);
colormap(gray);
This plots the matrix in a surface plot and sets the colormap to grayscale.
In addition, as I understand your data, you might be able to eliminate entire plane-segments in your matrix. If for instance the plane A(1,1:2000,1:1000) is NaN in all entries you could eliminate all those entries (thus the entire Y,Z-plane in entry X=1). This will however require some heavy for loops, which might be over the top. This depends on how many data matrices you have compared to how many different plot you want for each matrix.
I will try to give you some ideas. I assume lack of a direct 3D "surface detector".
Since you have a 3D matrix where XY-planes are CT scan slices and each slice is an image, I would try to find edges of each slice say with edge. This would require some preprocessing like first thresholding each slice image. Then I can either use scatter3 to display the edge data as a 3D point cloud or delaunay3 to display the edge data as a surface.
I hope this will help you achieve what you are asking for.
I managed to get it working:
function [X,Y,Z,C] = extract_surface(file_name,slice_number,voxel_size)
LT = imread(file_name);%..READ THE 2D MAP
BW = im2bw(LT,1);%..THRESHOLD TO BINARY
B = bwboundaries(BW,8,'noholes');%..FIND THE OUTLINE OF THE IMAGE
X = B{1}(:,1);%..EXTRACT X AND Y COORDINATES
Y = B{1}(:,2);
indices = sub2ind(size(LT),X,Y);%..FIND THE CORRESPONDING LINEAR INDICES
C = LT(indices);%..NOW READ THE VALUES AT THE OUTLINE POSITION
Z = ones(size(X))*slice_number;
I can then plot this with
figure
scatter3(X,Y,Z,2,C)
Now the only thing I could improve is to have all these points in the scatter plot connected with a surface. #upperBound you suggested delaunay3 for this purpose - I cannot quite figure out how to do this. Do you have a tip?

How do I draw 2D Map with plot() in 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.