How to plot the surface of an arbitrary given TABLE (not matrix) with X,Y,Z values?

Illustration
Emerson De Souza - 2023-03-22T10:21:42+00:00
Question: How to plot the surface of an arbitrary given TABLE (not matrix) with X,Y,Z values?

I want to plot the surface Z of arbitrary given values for each X,Y-pair.   I usually IMPORT the tables with the X,Y,Z data, so I they are not a matrix.   An example is displayed below:   1 1 0.171121066356432 1 2 0.0326008205305280 1 3 0.561199792709660 2 1 0.881866500451810 2 2 0.669175304534394 2 3 0.190433267179954 3 1 0.368916546063895 3 2 0.460725937260412 3 3 0.981637950970750 I tried the following lines to plot the surface [X,Y] = meshgrid(1:1:3, 1:1:3); Z=rand(9,1); surf(X,Y,Z)   But I get the following error:   ??? Error using ==> surf at 78 Z must be a matrix, not a scalar or vector.   Error in ==> Untitled2 at 5 surf(X,Y,Z) Question: I replaced the x,y coordinate with the function meshgrid but what do I make wih the Z values?   I'm aware that Z is not a matrix, but this is exactly the problem!!!

Expert Answer

Profile picture of Prashant Kumar Prashant Kumar answered . 2025-11-20

To plot the surface for a table with arbitrary X,Y,ZX, Y, Z values, you need to organize the ZZ values into a grid format that corresponds to X,YX, Y pairs. The function surf requires ZZ to be a 2D matrix, where each element corresponds to a specific X,YX, Y pair.

Here’s how you can handle this step by step:


Steps to Create the Surface Plot

  1. Prepare the Data: Import your table and extract X,Y,ZX, Y, Z columns.
  2. Convert X,Y,ZX, Y, Z into Gridded Format: Use griddata or accumarray to arrange ZZ values into a grid based on unique XX and YY values.
  3. Plot the Surface: Use surf to create the plot.

Code Example

Given Data:

 

% Define your table data
X = [1; 1; 1; 2; 2; 2; 3; 3; 3]; % X-coordinates
Y = [1; 2; 3; 1; 2; 3; 1; 2; 3]; % Y-coordinates
Z = [0.1711; 0.0326; 0.5612; 0.8819; 0.6692; 0.1904; 0.3689; 0.4607; 0.9816]; % Z-values

Solution:

% Create a grid of unique X and Y values
[Xq, Yq] = meshgrid(unique(X), unique(Y));

% Interpolate Z values into the grid using griddata
Zq = griddata(X, Y, Z, Xq, Yq);

% Plot the surface
surf(Xq, Yq, Zq);

% Add labels and title
xlabel('X');
ylabel('Y');
zlabel('Z');
title('Surface Plot from Arbitrary X, Y, Z Data');
colorbar;

Key Notes

  1. Data Structure:
    • X, Y, and Z should be column vectors, as in your example.
  2. griddata: This function interpolates scattered data into a regular grid, making it suitable for surface plotting.
  3. Visualization:
    • surf plots the 3D surface.
    • Add a colorbar for better visualization of ZZ-values.

Error Explanation

The error occurred because surf requires ZZ to be a matrix matching the dimensions of XX and YY. By using griddata to interpolate or organize ZZ, you convert it into a compatible format.

With this approach, you can handle any arbitrary X,Y,ZX, Y, Z data imported as a table!


Not satisfied with the answer ?? ASK NOW

Get a Free Consultation or a Sample Assignment Review!