Plot 3D surface from Excel .csv File

65 views (last 30 days)
Hello,
I am trying to plot 3D surface from the attached excel csv file (data.csv).
I want the surface to be smooth if possible.
  • x = first column;
  • y = second column;
  • z = third column.
I am new to Matlab and I have been struggling to do it. Could you please help me?
Also, how could I annotate the minumum of the surface and its x and y coordinates as well.
Thanks a lot!
-

Accepted Answer

Simon Chan
Simon Chan on 29 Aug 2021
You may extract the data using function readmatrix.
clear; clc;
rawdata = readmatrix('data.csv');
x = reshape(rawdata(:,1),[],51); % Reshape the column matrix into 51 columns
y = reshape(rawdata(:,2),[],51);
z = reshape(rawdata(:,3),[],51);
surf(x,y,z)
result as follows:
  7 Comments
Tom
Tom on 1 Mar 2023
hi, i'm trying to use this code to create my own contour plot from a csv file that has data in a 3x55 matrix.
is there any reason you converted the column matrix into specifically a 51 column matrix?
and how would i need to adapt this code to use with my csv file?
sorry, i am also very new to matlab but it's the only software i have access to that does what i need for a final year project.
thanks, t
Simon Chan
Simon Chan on 2 Mar 2023
You may spend some time to look at the attached csv file.
Actually there are totally 51x51 data but arranged in a column vector. On the other hand, function surf requires z-coordinates to be a matrix. So the conversion mainly converts the z-coordinates, which is the third column in this csv file into a matrix.
While for the x and y coordinates, they are both going from 0 to 0.5 with step size 0.01, and hence there are totally 51 data points. Without extracting the x and y-coordinates from the csv file, you may also use the following line to do the same thing.
[x,y] = meshgrid(0:0.01:0.5); % 2D grid for both 0:0.05:0.5
z = reshape(rawdata(:,3),length(0:0.01:0.5),length(0:0.01:0.5)); % Reshape z into a 51x51 matrix
surf(x,y,z);

Sign in to comment.

More Answers (0)

Products


Release

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!