Embedding 2d plot into a 3d plot in matlab? - matlab

I have a set of data vectors z that has this 2d plot
How would I go about embed this set of data into a 3d plot like this in matlab? I'm asking for advice and suggestions. The theory I'm trying to employ is "for each data vector~zj, “copies” the data vector intothe first two entries of a 3D data vector~yjand then computes the squared length of~zj as the third entry of~yj. " or kernel trick.

Your 2d data will somehow be in the form, that you have x-coordinates and y-coordinates. Let's say you have a vector x and a vector y for simplification.
As you found out the plot3-function proivdes functionality to plot arbitrary points in 3d without the need of generating a mesh. What you need additionally is a third vector z with data for the 3rd dimension.
So what else can you do? The thing I am thinking about is rotating the plane you are plotting you "2d" data:
Rotational matrices can be seen here:
https://en.wikipedia.org/wiki/Rotation_matrix

Related

Plotting arbitrary 3d finite element mesh with matlab

Hello guys I am trying to export a mesh from MSC Patran and then plot it in Matlab. The mesh can be of arbitrary shape. I have the x, y and z coordinates of all the nodes. So far I have tried many different options and here is why they failed:
Surfc() with meshgrid and griddata:
I generated a grid on x-y plane with meshgrid and then used griddata to obtain the z matrix. But this plot only works when there is only 1 z value corresponding to an x-y pair. In other words, for this to work z must be of type z = f(x,y).
pdegplot() : I found out that matlab can import and plot .stl files. I tried converting my coordinate matrix format and plot it with this function but it doesn't work either. Because apparently in .stl files an edge can not be shared by more than 2 elements. However my FEM files are always (i hope) shell elements. This means 3 or more elements can share the same elements.
Surfc() with 3d meshgrid: I found out that meshgrid() can take 3 inputs (x,y,z) and create a 3d mesh. However this didn't work either. I used a very small mesh with about 1000 nodes and the code was trying to generate 3 matrices with 1000x1000x1000 elements. That means about 3 gb of memory for a 1000 node mesh. Whats more, surfc couldn't plot even that.
Somehow importing other file formats automatically: so far I have been using patran neutral files (.out). I manually read the file and extract x,y,z data from it. Patran can also export to parasolid, iges and step file formats. I looked for direct ways of importing and plotting these in matlab but such functions don't exist as far as I have looked.
Manually generating a grid: Matlab can create 3D objects (like [x,y,z] = sphere()) and Surfc() can plot these despite what I said in (1.) and the x,y,z matrices generated by sphere() are not 3 dimensional like in (3.) so I tried following this and manually generate a 3d grid from my FEM file just for testing. I found that z has repeating columns and in each column (which acts as a layer) there are n values of x and y. When I tried doing the same thing for my mesh manually, surfc() didn't work again. It plotted a really weird shape that I can't even describe.
Finding a 3rd party plotting software: I tried using (light) software like gnuplot and visit but so far I am all wet. I am open to suggestions if you know any (preferably open source) software that can directly plot patran neutral files. But the software has to be capable of contour plotting also. As I am calculating a quantity for each node in Matlab and then plotting its contour on the mesh.
So you can have a tetramesh?
You seem to be working with FEM-stile meshes, thus the standard surface-plotting function wont work. For FEM-meshes of different shape (not tetra) you may need to write your own function...
If you have the grid points and grid cell connectivities in say variables p and c, you can use the external Matlab FEA Toolbox to plot both structured and unstructured grids with for example the plotgrid command
% Cread grid struct.
grid.p = p;
grid.c = c;
grid.a = gridadj(p,c,size(p,1)); % Reconstruct grid cell adjacencies.
grid.b = gridbdr(p,c,grid.a); % Reconstruct boundary information.
grid.s = ones(1,size(c,2)); % Set subdomain numbers to 1 for all grid cells.
% Plot grid.
plotgrid( grid )

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.

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?

Plotting 3-D Matrix *values* in MATLAB

I have a 3D Matrix M(256x256x136) and each index(i,j,k) in M has a gray level value in it. I am interested in displaying M in some sort of a 3D plot in MATLAB, but am unable to do so. I cannot use plot3 because plot3 is for plotting points, not the values.
Thanks
If I understand your question correctly, you want to plot the 3D point cloud with i,j, and k as 3D coordinates and the gray level as the point value.
I would suggest using scatter3.
Sounds like you are looking for a volume renderer. For Matlab, you could try this one: Volume Render from Matlab Central
An isosurface plot might be useful as well.