Unable to plot 3D graph
2 次查看(过去 30 天)
显示 更早的评论
I am trying to plot velocity data contained within a 2D plane (x-y). I would like to plot a 3D graph to present this. However, when trying to plot, I get an error stating
'Error using surf
Z must be a matrix, not a scalar or vector.'
The data is imported from an excel file and the appropriate columns are turned into arrays of x, y and U as shown below:
tbl = readtable('2D1mat.xlsx');
x=tbl.Var1;
y=tbl.Var2;
U=tbl.Var3;
surf(x,y,U)
I have also ensured that the input arguments for surf are all matrices with the help of ismatrix.
0 个评论
采纳的回答
Torsten
2022-11-7
Doesn't look very impressive, does it ?
A = readmatrix("https://de.mathworks.com/matlabcentral/answers/uploaded_files/1184273/2D1mat.xlsx");
x = A(:,1);
y = A(:,2);
u = A(:,3);
F = scatteredInterpolant(x,y,u);
xq = 33.5:1:53.5;
yq = 45:0.25:48.25;
[XQ,YQ] = meshgrid(xq,yq);
UQ = F(XQ,YQ);
surf(XQ,YQ,UQ)
2 个评论
Torsten
2022-11-7
编辑:Torsten
2022-11-7
Or the adapted solution from Star Strider:
A = readmatrix("https://de.mathworks.com/matlabcentral/answers/uploaded_files/1184273/2D1mat.xlsx");
x = A(:,1);
y = A(:,2);
z = A(:,3);
N = 100;
xv = linspace(min(x), max(x), N);
yv = linspace(min(y), max(y), N);
[X,Y] = ndgrid(xv, yv);
Z = griddata(x, y, z, X, Y, 'linear');
figure
surfc(X, Y, Z)
grid on
colormap(turbo)
colorbar
xlabel('X')
ylabel('Y')
zlabel('Z')
更多回答(1 个)
Star Strider
2022-11-7
The ismatrix function wil return true for a vector and for a 2D matrix. It returns false for arrays with more than 2 dimensions.
The vectors may actually represent gridded data and so would form matrices when the vectors are appropriately reshaped.
An approach that may work (since we do not have the vectors to work with) —
x = rand(1,10);
y = rand(1,10);
z = rand(1,10);
N = 25;
xv = linspace(min(x), max(x), N);
yv = linspace(min(y), max(y), N);
[X,Y] = ndgrid(xv, yv);
Z = griddata(x, y, z, X, Y, 'linear');
figure
surfc(X, Y, Z)
grid on
colormap(turbo)
colorbar
xlabel('X')
ylabel('Y')
zlabel('Z')
.
2 个评论
Star Strider
2022-11-7
There is actually no need to interpolate this, since the data are gridded, as the stem3 plot demonstrates.
For some reason, ‘Var2’ does not work with reshape as it should, despite a fair amount of effort to get it to. I finally gave up on that and went with repmat. Anyway, if all goes well, all the matrices are (14x21) and the surface plots correctly.
I checkked the surf plot against the stem3 plot, and they essentially agree. (The log transformation is necessary to get the stem3 plot to show any variation in Z, since the magnitudes of the third column don’t vary much. Without it, the stem3 plot hides what little variation there is.)
T1 = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1184273/2D1mat.xlsx')
figure
stem3(T1{:,1}, T1{:,2}, T1{:,3}, '.')
hold on
scatter3(T1{:,1}, T1{:,2}, T1{:,3}, 25, T1{:,3}, 'filled')
hold off
grid on
colormap(turbo)
xlabel('X')
ylabel('Y')
zlabel('Z')
set(gca, 'ZScale','log')
% view(0,90)
[U1,ix1] = unique(T1{:,1});
U1
rowlen = numel(U1)
[U2,ix2] = unique(T1{:,2});
U2
collen = numel(U2)
% X = reshape(T1{:,1}, rowlen, collen)
% Y = reshape(T1{:,2}, rowlen, collen)
X = repmat(U1,1,collen).';
Y = repmat(U2,1,rowlen);
Z = reshape(T1{:,3}, collen, rowlen);
figure
surfc(X, Y, Z)
grid on
colormap(turbo)
xlabel('X')
ylabel('Y')
zlabel('Z')
set(gca, 'ZScale','log')
% shading('interp')
.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Distribution Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!