Problem with reshape and Surf plot

조회 수: 6 (최근 30일)
ragnor
ragnor 2021년 7월 26일
댓글: Star Strider 2021년 7월 28일
Hello MATLAB Community,
I have the following script and data where i would like to plot a 3d Surface and it works well for some data and it doesn't work because of the dimensions in some cases. Can someone please suggest where is the mistake in the script and how to fix it?
opts = detectImportOptions('Data123.xlsx');
opts = opts.setvartype('Y','double');
opts = opts.setvartype('Z','double');
% Load data
data = readtable('Data123.xlsx', opts);
data.Properties.VariableNames = ["X","F_sw","Y","Z"];
% order the data
data = sortrows(data,["X","Y"]);
X = unique(data.X);
Y = unique(data.Y);
Z = reshape(data.Z,[length(Y),length(X)]);
% View
figure(1)
scatter3(data.X,data.Y,data.Z,12,[1 0 0],"filled","MarkerEdgeColor",[0 0 0])
hold on
s = surf(X,Y,Z,"FaceColor",'interp');
hold off
  댓글 수: 6
ragnor
ragnor 2021년 7월 26일
If i don't use unique, is there any other way, i can create a surface plot with the data.
I just want to make a 3d surface plot to display the results.
Thanks in advance.
ragnor
ragnor 2021년 7월 26일
I just want to make a 3D surface plot as displayed in the attached figure.

댓글을 달려면 로그인하십시오.

채택된 답변

Star Strider
Star Strider 2021년 7월 27일
The only solution I can propose is to use either griddata or scatteredInterpolant .
Try this —
opts = detectImportOptions('https://www.mathworks.com/matlabcentral/answers/uploaded_files/695269/Data123.xlsx');
opts = opts.setvartype('Y','double');
opts = opts.setvartype('Z','double');
% Load data
data = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/695269/Data123.xlsx', opts)
data = 184×4 table
X F_sw Y Z _____ ____ ___ ______ 10000 350 0.5 2.9008 10000 350 1 3.0802 10000 350 1.5 3.2261 10000 350 2 3.3174 10000 350 2.5 3.3299 10000 350 3 3.3618 10000 350 3.5 3.4404 10000 350 4 3.5091 10000 350 4.5 3.5816 10000 350 5 3.6302 10000 350 5.5 3.6945 10000 350 6 3.7262 10000 350 6.5 3.7544 10000 350 7 3.7761 10000 350 7.5 3.7944 10000 350 8 3.8067
data.Properties.VariableNames = ["X","F_sw","Y","Z"];
% order the data
data = sortrows(data,["X","Y"]);
% X = unique(data.X);
% Y = unique(data.Y);
X = data.X;
Y = data.Y;
Z = data.Z;
N = 50;
xv = linspace(min(X), max(X), N);
yv = linspace(min(Y), max(Y), N);
[Xm,Ym] = ndgrid(xv, yv);
Zm = griddata(X,Y,Z,Xm,Ym);
% Z = reshape(data.Z,[length(Y),length(X)]);
% View
figure(1)
scatter3(data.X,data.Y,data.Z,12,[1 0 0],"filled","MarkerEdgeColor",[0 0 0])
hold on
s = surf(Xm,Ym,Zm,"FaceColor",'interp', 'EdgeColor','none');
hold off
If it is not the result you want, I will delete my Answer.
.
  댓글 수: 4
ragnor
ragnor 2021년 7월 28일
@Star Strider: Thank you very much. It was indeed very uselful and it is something i wanted to plot.
Thanks again.
Star Strider
Star Strider 2021년 7월 28일
As always, my pleasure!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by