Unable to plot 3D graph

2 Ansichten (letzte 30 Tage)
vauntedmango
vauntedmango am 7 Nov. 2022
Kommentiert: Star Strider am 7 Nov. 2022
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.

Akzeptierte Antwort

Torsten
Torsten am 7 Nov. 2022
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 Kommentare
vauntedmango
vauntedmango am 7 Nov. 2022
Not impressive at all, but atleast I know what to work on now :D
Thank you very much for the solution!
Torsten
Torsten am 7 Nov. 2022
Bearbeitet: Torsten am 7 Nov. 2022
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')

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Star Strider
Star Strider am 7 Nov. 2022
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 Kommentare
vauntedmango
vauntedmango am 7 Nov. 2022
Thank you for your response. However, I am still struggling to present this data. Just to clarify, the dataset being imported consists of experimental measurements of mean velocity in a 2D plane. This was done by following a grid pattern (this means there are repeating values in x and in y when looking at the columns individually). I have attached the Excel file containing the data. Column 1, 2 and 3 correspond to X, Y and mean velocity respectively. I would much appreciate it if you can go through that too
Star Strider
Star Strider am 7 Nov. 2022
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')
T1 = 294×3 table
Var1 Var2 Var3 ____ _____ ______ 33.5 45 2.2377 33.5 45.25 2.2382 33.5 45.5 2.2408 33.5 45.75 2.239 33.5 46 2.2403 33.5 46.25 2.2397 33.5 46.5 2.2407 33.5 46.75 2.239 33.5 47 2.2378 33.5 47.25 2.2401 33.5 47.5 2.2343 33.5 47.75 2.2339 33.5 48 2.2378 33.5 48.25 2.2332 34.5 48.25 2.2385 34.5 48 2.2376
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
U1 = 21×1
33.5000 34.5000 35.5000 36.5000 37.5000 38.5000 39.5000 40.5000 41.5000 42.5000
rowlen = numel(U1)
rowlen = 21
[U2,ix2] = unique(T1{:,2});
U2
U2 = 14×1
45.0000 45.2500 45.5000 45.7500 46.0000 46.2500 46.5000 46.7500 47.0000 47.2500
collen = numel(U2)
collen = 14
% 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')
.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Data Distribution Plots finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by