How to surface or mesh plot imported data?

Hi!
I am new to matlab and could not find answer to how to import data organised like a matrix with a space as delimiter. It looks like this
x1 y1 z11
x1 y2 z12
x1 y3 z13 .... (etc)
until the last row
xn y1 zn1
xn y2 zn2
...
xn yn znn
I would like to have the x and y coordinates in the plane and the z coordinates as the height above the plane, either in a surf or a mesh plot, but all tutorials and help articles I find are for excel or use code that throws errors when I try to use them.
All help is appreciated! Thanks! :-)

 Respuesta aceptada

Star Strider
Star Strider el 21 de Sept. de 2023

0 votos

... I need some commands that instructs MatLab that VarName1 and VarName2 are x and y respectively, while VarName3 is the z-coordinate.
Perhaps something like this —
testout = readtable('YourExcelFile.xlsx')
testout.Properties.VariableNames = {'x','y','z'};
X = reshape(testout.x, 21,[]);
Y = reshape(testout.y, 21,[]);
Z = reshape(testout.z, 21,[]);
figure
plot(X, Y, Z)
xlabel('X')
ylabel('Y)
zlabel('Z')
Make appropriate changes to work with your file and data.
.

2 comentarios

Daniel Bozi
Daniel Bozi el 21 de Sept. de 2023
Wow that really helped, thank you! Especially the readtable command that I somehow missed earlier. Oh and I changed plot to surf :-)
Star Strider
Star Strider el 21 de Sept. de 2023
As always, my pleasure!
My using plot instead of surf was an oversight. My apologies! (I need to proofread my code here before posting it.)

Iniciar sesión para comentar.

Más respuestas (1)

KSSV
KSSV el 21 de Sept. de 2023

0 votos

3 comentarios

Daniel Bozi
Daniel Bozi el 21 de Sept. de 2023
Thank you! My problem is that the data file is quite large (several hundred rows), so I need some commands that instructs MatLab that VarName1 and VarName2 are x and y respectively, while VarName3 is the z-coordinate. The surface and mesh plot options are not available since MatLab does not recognise the x and y columns as coordinates, and VarName3 as entries in a matrix.
Here is what the start of the file looks like:
Daniel Bozi
Daniel Bozi el 21 de Sept. de 2023
btw just to add, the matrix would be of size 21x21 (sqrt(441) which is the table size)
KSSV
KSSV el 21 de Sept. de 2023
Read about @doc:readtable

Iniciar sesión para comentar.

Preguntada:

el 21 de Sept. de 2023

Comentada:

el 21 de Sept. de 2023

Community Treasure Hunt

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

Start Hunting!

Translated by