Create surface plot from txt file with x y z coordinates.

26 views (last 30 days)
I am trying to plot a 3D surface from the x y and z coordinates that I obtain in a txt file with three columns representing x, y and z values.
I can extract the x, y and z values and create 3 seperate column matrix and then I try to plot the x, y and z using the surf function but it returns an error stating - "Z must be a matrix, not a scalar or vector". However it plots correctly if I use the command plot3(x,y,z) but it does not creates a surface and also in plot3 z has no negative values.
I will insert the code here,
clear all;
close all;
clc
A=dlmread('mode shape 1.txt');
x=A(:,1);
y=A(:,2);
[X,Y]=meshgrid(x,y);
Z=A(:,3);
surf(X,Y,Z)
Error using dlmread
The file 'mode shape 1.txt' could not be opened because: No such file or directory
  1 Comment
Torsten
Torsten on 14 Jun 2022
Edited: Torsten on 14 Jun 2022
Z must be a matrix, not a vector since Z(i,j) = Z(x(i),y(j))
plot3 plots a curve, not a surface. If x,y and z are vectors, then you cannot create a surface out of them.

Sign in to comment.

Answers (1)

Sayan
Sayan on 30 Aug 2023
I understand that a 3D surface plot needs to be plotted from the x, y, and z coordinates extracted from the text file. However, here creating a "meshgrid" is not required. Only plotting the x, y, and z data with the "surf" function will work. "Z" is just a column vector whereas "X" and "Y" are matrices with size equal to the product between the "length(y)" and "length(x)". It can be plotted as shown below.
A=dlmread('mode shape 1.txt');
x=A(:,1);
y=A(:,2);
z=A(:,3);
surf(x,y,z)
For further assistance with the "surf" and "meshgrid" functions you can refer to the following MATLAB documentation:
Hope this helps in resolving the issue.

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!