Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Note that the function <a href="https://www.mathworks.com/help/matlab/ref/trisurf.html" rel="nofollow noreferrer"><code>trisurf</code></a> that you were originally trying to use returns a handle to a <em>patch</em> object. If you look at the <a href="https://www.mathworks.com/help/matlab/ref/patch-properties.html#property_d0e724248" rel="nofollow noreferrer"><code>'FaceColor'</code> property for patch objects</a>, you can see that there is no <code>'texturemap'</code> option. That option is only valid for the <a href="https://www.mathworks.com/help/matlab/ref/primitivesurface-properties.html#property_d0e891374" rel="nofollow noreferrer"><code>'FaceColor'</code> property of <em>surface</em> objects</a>. You will therefore have to find a way to plot your triangular surface as a <em>surface</em> object instead of a <em>patch</em> object. Here are two ways to approach this:</p> <h2>If your data is in a uniform grid...</h2> <p>If the coordinates of your surface data represent a uniform grid such that <code>z</code> is a rectangular set of points that span from <code>xmin</code> to <code>xmax</code> in the x-axis and <code>ymin</code> to <code>ymax</code> in the y-axis, you can plot it using <a href="https://www.mathworks.com/help/matlab/ref/surf.html" rel="nofollow noreferrer"><code>surf</code></a> instead of <a href="https://www.mathworks.com/help/matlab/ref/trisurf.html" rel="nofollow noreferrer"><code>trisurf</code></a>:</p> <pre><code>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 </code></pre> <p>In order to illustrate the results of the above code, I initialized the data as <code>Z = peaks;</code>, used the built-in sample image <code>'peppers.png'</code>, and set the <code>x</code> and <code>y</code> values to span from 1 to 16. This resulted in the following texture-mapped surface:</p> <p><a href="https://i.stack.imgur.com/gxZNt.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/gxZNt.jpg" alt="enter image description here"></a></p> <h2>If your data is non-uniformly spaced...</h2> <p>If your data are not regularly spaced, you can create a set of regularly-spaced <code>X</code> and <code>Y</code> coordinates (as I did above using <a href="https://www.mathworks.com/help/matlab/ref/meshgrid.html" rel="nofollow noreferrer"><code>meshgrid</code></a>) and then use one of the functions <a href="https://www.mathworks.com/help/matlab/ref/griddata.html" rel="nofollow noreferrer"><code>griddata</code></a> or <a href="https://www.mathworks.com/help/matlab/ref/triscatteredinterp.html" rel="nofollow noreferrer"><code>TriScatteredInterp</code></a> to interpolate a regular grid of <code>Z</code> values from your irregular set of <code>z</code> values. I discuss how to use these two functions in <a href="https://stackoverflow.com/a/1674574/52738">my answer to another SO question</a>. Here's a refined version of the code you posted using <code>TriScatteredInterp</code> (Note: as of R2013a <a href="https://www.mathworks.com/help/matlab/ref/scatteredinterpolant-object.html" rel="nofollow noreferrer"><code>scatteredInterpolant</code></a> is the recommended alternative):</p> <pre><code>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 </code></pre> <p>In this case, you have to <em>first</em> choose the values of <code>N</code> and <code>M</code> for the size of your matrix <code>Z</code>. In order to illustrate the results of the above code, I initialized the data for <code>x</code>, <code>y</code>, and <code>z</code> as follows and used the built-in sample image <code>'peppers.png'</code>:</p> <pre><code>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 </code></pre> <p>This resulted in the following texture-mapped surface:</p> <p><a href="https://i.stack.imgur.com/dnKlJ.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/dnKlJ.jpg" alt="enter image description here"></a></p> <p>Notice that there are jagged edges near the corners of the surface. These are places where there were too few points for <code>TriScatteredInterp</code> to adequately fit an interpolated surface. The <code>Z</code> values at these points are therefore <a href="https://www.mathworks.com/help/matlab/ref/nan.html" rel="nofollow noreferrer"><code>nan</code></a>, resulting in the surface point not being plotted.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload