Plot 3D surface from Excel .csv File

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

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

thanks a lot! is there a way to annotate the minimum point?
Minimum points with red marker. Or you would like to annotate with text?
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)
minvalue = min(min(z)); % Find the minimum value
[row,col] = find(z==minvalue); % Find the location of minimum points
surf(x,y,z,'FaceAlpha',0.5,'EdgeColor','none')
hold on
plot3(x(row,col),y(row,col),z(row,col),'r*','MarkerSize',10); % Plot the red markers
xlabel('x-axis') % Labels (you may modify)
ylabel('y-axis')
zlabel('Potential Energy')
Thanks a lot! this is perfect and I now see how you calculated the minimum value and its corresponding coordinates! thans again.
Sure.
The minimum vlaue is stored in variable 'minvalue', you may type this variable name in Command Window and then gives the answer. In your case 234.55.
The x and y coordinates are stored in variable 'row' and 'col', again type these name in the Command Window and you can get row = 26 & 26 and col = 20 & 21. Hence there are 2 minimum points located on row #26 and column #20 & #21.
If you want to get back the value of x and y in these corrdinates, type x(row, col) and y(row,col) and in this case, the value of x is 0.25, while value of y are 0.19 & 0.20.
On the other hand, type z(row,col) can give you the minimum value as well.
I really appreciate. I got a better understanding of how Matlab works from your answers as well. Thanks!
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
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

Asked:

on 29 Aug 2021

Commented:

on 2 Mar 2023

Community Treasure Hunt

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

Start Hunting!