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

34 次查看(过去 30 天)
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 个评论
Torsten
Torsten 2022-6-14
编辑:Torsten 2022-6-14
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.

请先登录,再进行评论。

回答(1 个)

Sayan
Sayan 2023-8-30
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.

类别

Help CenterFile Exchange 中查找有关 Surface and Mesh Plots 的更多信息

标签

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by