How to export a 3D surface (gyroid) into an STL file - matlab

How do I export this Matlab code into an STL file for Solidworks and 3D printing?
[x, y, z] = meshgrid (-pi:pi/16:pi); % 3D coordinates defined by x, y, z
v = sin(x).*cos(y)+sin(y).*cos(z)+sin(z).*cos(x); %gyroid formula
t=0.5+0.1*z; % offset from isovalue, affects thickness
v=(v-t).*(v+t); %multiplies one negative offset gyroid and one positive offset gyroid
figure(1)
isosurface (x, y, z, v, 0);
hold on
figure(1)
isocaps(x,y,z,v,0,"below");

Related

shadow an area on contour matlab

I'm tiring to plot contour map like picture is following below , i can plot the contour, but problem is with shadow area on contour . actually i have 4 vector data [x,y,z,k]. x,y area coordinates and z , k are levels .
my code:
sample=xlsread('sample.xlsx');
x=(sample(:,1));
y=(sample(:,2));
z=(sample(:,3));
k=sample(:,4);
N=50;
[X,Y]=meshgrid(linspace(min(x)-0.2, max(x)+0.2, N), linspace(min(y)-0.2, max(y)+0.2, N));
F = scatteredInterpolant(x, y, z);
FF=scatteredInterpolant(x, y, k);
Z= F(X,Y);
ZZ=FF(X,Y);
contour(X,Y,Z,'ShowText','on') %main contour
hold on
???? shadow???
How should do this ?? thanks
If you have the X-Y points of the "shadow area", you can use the fill command (documentation)
fill(X_Area,Y_Area,'b')

How to display a 2-D matrix using surf or mesh?

Let's say I have a matrix A, how can I display it with surf or mesh that X-Y axies are the index of elements(e.g. i,j) and the Z value is the values in A(i,j)?
You can just pass it directly to surf and it will automatically use the indices as the x and y coordinates
data = rand(10)
surf(data);
surf(Z) creates a three-dimensional shaded surface from the z components in matrix Z, using x = 1:n and y = 1:m, where [m,n] = size(Z). The height, Z, is a single-valued function defined over a geometrically rectangular grid. Z specifies the color data, as well as surface height, so color is proportional to surface height. The values in Z can be numeric or datetime or duration values.

A 3D plot in MATLAB

I have two variables which i sweep, W1 and W3. I made a nested loop of these two variables.
for i=1:size(W1,2)
for j=1:size(W3,2)
d(i,j)=someexpression(W1(i),W3(j))
end
end
I want to do a 3D plot with W1 in the x-axis and W3 in the y-axis and d should be in the z-axis so that I have a 3D plot (or some contour plot).
EDIT: The 3d plot should actually be a surface
You can do the interpolation manually:
x = linspace(W1(1), W1(end), 100);
y = linspace(W2(1), W2(end), 100);
z = interp2(W1, W2, d, x, y);
surf(x, y, z)

Plot a curve rather than a line

In matlab, I have a vector, say x and a function of x say y. I want to plot x and y in matlab.
The problem is I want smooth curve (not in a sense of smooth texture but differentiable, and without sharp bends). Matlab, with plot, simply joins the points and the plotted curve has sharp bends.
Is there a way I can resolve this?
Following Dan and wakjah, what you need is to interpolate x and y to more sample points
plot( x, y, '+r' ); % plot the original points
n = numel(x); % number of original points
xi = interp1( 1:n, x, linspace(1, n, 10*n) ); % new sample points
yi = interp1( x, y, xi );
hold all;
plot( xi, yi ); % should be smooth between the original points

Texture mapping in MATLAB

I have points in 3D space and their corresponding 2D image points. How can I make a mesh out of the 3D points, then texture the triangle faces formed by the mesh?
Note that the function trisurf that you were originally trying to use returns a handle to a patch object. If you look at the 'FaceColor' property for patch objects, you can see that there is no 'texturemap' option. That option is only valid for the 'FaceColor' property of surface objects. You will therefore have to find a way to plot your triangular surface as a surface object instead of a patch object. Here are two ways to approach this:
If your data is in a uniform grid...
If the coordinates of your surface data represent a uniform grid such that z is a rectangular set of points that span from xmin to xmax in the x-axis and ymin to ymax in the y-axis, you can plot it using surf instead of trisurf:
Z = ... % N-by-M matrix of data
x = linspace(xmin, xmax, size(Z, 2)); % x-coordinates for columns of Z
y = linspace(ymin, ymax, size(Z, 1)); % y-coordinates for rows of Z
[X, Y] = meshgrid(x, y); % Create meshes for x and y
C = imread('image1.jpg'); % Load RGB image
h = surf(X, Y, Z, flipdim(C, 1), ... % Plot surface (flips rows of C, if needed)
'FaceColor', 'texturemap', ...
'EdgeColor', 'none');
axis equal
In order to illustrate the results of the above code, I initialized the data as Z = peaks;, used the built-in sample image 'peppers.png', and set the x and y values to span from 1 to 16. This resulted in the following texture-mapped surface:
If your data is non-uniformly spaced...
If your data are not regularly spaced, you can create a set of regularly-spaced X and Y coordinates (as I did above using meshgrid) and then use one of the functions griddata or TriScatteredInterp to interpolate a regular grid of Z values from your irregular set of z values. I discuss how to use these two functions in my answer to another SO question. Here's a refined version of the code you posted using TriScatteredInterp (Note: as of R2013a scatteredInterpolant is the recommended alternative):
x = ... % Scattered x data
y = ... % Scattered y data
z = ... % Scattered z data
xmin = min(x);
xmax = max(x);
ymin = min(y);
ymax = max(y);
F = TriScatteredInterp(x(:), y(:), z(:)); % Create interpolant
N = 50; % Number of y values in uniform grid
M = 50; % Number of x values in uniform grid
xu = linspace(xmin, xmax, M); % Uniform x-coordinates
yu = linspace(ymin, ymax, N); % Uniform y-coordinates
[X, Y] = meshgrid(xu, yu); % Create meshes for xu and yu
Z = F(X, Y); % Evaluate interpolant (N-by-M matrix)
C = imread('image1.jpg'); % Load RGB image
h = surf(X, Y, Z, flipdim(C, 1), ... % Plot surface
'FaceColor', 'texturemap', ...
'EdgeColor', 'none');
axis equal
In this case, you have to first choose the values of N and M for the size of your matrix Z. In order to illustrate the results of the above code, I initialized the data for x, y, and z as follows and used the built-in sample image 'peppers.png':
x = rand(1, 100)-0.5; % 100 random values in the range -0.5 to 0.5
y = rand(1, 100)-0.5; % 100 random values in the range -0.5 to 0.5
z = exp(-(x.^2+y.^2)./0.125); % Values from a 2-D Gaussian distribution
This resulted in the following texture-mapped surface:
Notice that there are jagged edges near the corners of the surface. These are places where there were too few points for TriScatteredInterp to adequately fit an interpolated surface. The Z values at these points are therefore nan, resulting in the surface point not being plotted.
If your texture is already in the proper geometry you can just use regular old texture mapping.
The link to the MathWorks documentation of texture mapping:
http://www.mathworks.com/access/helpdesk/help/techdoc/visualize/f0-18164.html#f0-9250
Re-EDIT: Updated the code a little:
Try this approach (I just got it to work).
a=imread('image.jpg');
b=double(a)/255;
[x,y,z]=peaks(30); %# This is a surface maker that you do have
%# The matrix [x,y,z] is the representation of the surface.
surf(x,y,z,b,'FaceColor','texturemap') %# Try this with any image and you
%# should see a pretty explanatory
%# result. (Just copy and paste) ;)
So [x,y,z] is the 'surface' or rather a matrix containing a number of points in the form (x,y,z) that are on the surface. Notice that the image is stretched to fit the surface.