MATLAB Answers

Problem with reshape and Surf plot

2 views (last 30 days)
ragnor
ragnor on 26 Jul 2021
Commented: Star Strider on 28 Jul 2021
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 Comments
ragnor
ragnor on 26 Jul 2021
I just want to make a 3D surface plot as displayed in the attached figure.

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 27 Jul 2021
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 Comments

Sign in to comment.

More Answers (0)

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!