3D plot from excel

8 次查看(过去 30 天)
Hello,
I want to get a 3D plot for the stresses (z coordinates) on a xy plane (see the excel attached).
I developed the following code:
T1 = readtable('URM_shear.xlsx', 'VariableNamingRule','preserve');
% First10Rows = T1(1:10,:)
% T1Sz = size(T1)
VarNames = T1.Properties.VariableNames;
N = 50; % Interpolation Matrix Size
xv = linspace(min(T1{:,1}), max(T1{:,1}), N); % Create Vector
yv = linspace(min(T1{:,2}), max(T1{:,2}), N); % Create Vector
[Xm,Ym] = ndgrid(xv,yv); % Create Interpolation Matrices
Zm = griddata(T1{:,1}, T1{:,2}, T1{:,3}, Xm, Ym); % Interpolate
figure
surfc(Xm, Ym, Zm)
grid on
shading interp
set(0, 'DefaultTextInterpreter', 'latex')
hcb = colorbar;
xlabel(hcb,'[{\it Pa}]');
hcb.Label.Interpreter = 'latex';
set(hcb,'TickLabelInterpreter','latex')
colormap(jet(50))
title ('EPX1','FontSize',13,'interpreter', 'latex')
set(0, 'DefaultTextInterpreter', 'latex')
hcb = colorbar;
hcb.TickLabelInterpreter='latex';
% ColorbarRulerProperties = hcb.Ruler
hcb.Ruler.TickLabelFormat = '%.2f';
set(hcb, 'Ticks', sort([hcb.Limits, hcb.Ticks]))
set(gca,'TickLabelInterpreter','latex')
% tix = hcb.Ticks;
% expstr = @(x) [x(:).*10.^ceil(-log10(abs(x(:)+(x==0)))) floor(log10(abs(x(:)+(x==0))))];
% tixexp = expstr(tix(:))
% tixexplbl = compose('%.2f \\times 10^{%2d}',[tixexp(:,1) fix(tixexp(:,2))])
% hcb.TickLabels = tixexplbl;
xlabel('{\it x} [{\it mm}]')
ylabel('{\it y} [{\it mm}]')
zlabel('{\it} Shear stress [{\it MPa}]')
Can someone help me with this?
Thank you

采纳的回答

Star Strider
Star Strider 2021-11-20
Thanks for quoting my code!
The second column ‘Y Coordinate (mm)’ is identically zero. The griddata function will of course have a problem with that, since it wants vectors with variations, not contant values. So ‘Y Coordinate (mm)’ needs to be something other than a vector of zeros. I have no idea what the original problem is wth it, or if it is empirical (experimental) data or calculated data.
Nothing can be done with the matrix as it currently exists. It needs to have non-constant (and preferably unique) values in each variable. The matrix needs to be updated, or a new matrix substituted for it.
T1 = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/807619/URM_shear.xlsx', 'VariableNamingRule','preserve')
T1 = 46208×3 table
X Coordinate (mm) Y Coordinate (mm) Shear Stress (MPa) _________________ _________________ __________________ 0 0 -3.4657 -0.168 0 -7.6145 -0.168 0 -8.0765 0 0 -3.9925 -0.168 0 -8.0765 -0.168 0 -8.5383 0 0 -4.5192 0 0 -3.9925 0 0 -4.5192 0.167 0 -0.50006 0.167 0 0.091478 0 0 -3.9925 0.167 0 0.091478 0.167 0 0.68301 0 0 -3.4657 0 0 -3.9925
QQ1 = 2×3
-12.5330 0 -8.5386 0.1670 0 0.6837
Stats = varfun(@(x)[min(x); max(x)], T1)
Stats = 2×3 table
Fun_X Coordinate (mm) Fun_Y Coordinate (mm) Fun_Shear Stress (MPa) _____________________ _____________________ ______________________ -12.533 0 -8.5386 0.167 0 0.68366
% First10Rows = T1(1:10,:)
% T1Sz = size(T1)
VarNames = T1.Properties.VariableNames;
N = 50; % Interpolation Matrix Size
xv = linspace(min(T1{:,1}), max(T1{:,1}), N); % Create Vector
yv = linspace(min(T1{:,2}), max(T1{:,2}), N); % Create Vector
[Xm,Ym] = ndgrid(xv,yv); % Create Interpolation Matrices
Zm = griddata(T1{:,1}, T1{:,2}, T1{:,3}, Xm, Ym); % Interpolate
Warning: Duplicate data points have been detected and removed - corresponding values have been averaged.
Warning: The underlying triangulation is empty - the points may be collinear.
figure
surfc(Xm, Ym, Zm)
Error using surf (line 71)
Data dimensions must agree.

Error in surfc (line 54)
hs = surf(cax, args{:});
grid on
shading interp
set(0, 'DefaultTextInterpreter', 'latex')
hcb = colorbar;
xlabel(hcb,'[{\it Pa}]');
hcb.Label.Interpreter = 'latex';
set(hcb,'TickLabelInterpreter','latex')
colormap(jet(50))
title ('EPX1','FontSize',13,'interpreter', 'latex')
set(0, 'DefaultTextInterpreter', 'latex')
hcb = colorbar;
hcb.TickLabelInterpreter='latex';
% ColorbarRulerProperties = hcb.Ruler
hcb.Ruler.TickLabelFormat = '%.2f';
set(hcb, 'Ticks', sort([hcb.Limits, hcb.Ticks]))
set(gca,'TickLabelInterpreter','latex')
% tix = hcb.Ticks;
% expstr = @(x) [x(:).*10.^ceil(-log10(abs(x(:)+(x==0)))) floor(log10(abs(x(:)+(x==0))))];
% tixexp = expstr(tix(:))
% tixexplbl = compose('%.2f \\times 10^{%2d}',[tixexp(:,1) fix(tixexp(:,2))])
% hcb.TickLabels = tixexplbl;
xlabel('{\it x} [{\it mm}]')
ylabel('{\it y} [{\it mm}]')
zlabel('{\it} Shear stress [{\it MPa}]')
.
  4 个评论
Star Strider
Star Strider 2021-11-20
As always, my pleasure!
This time, it works!
Is this plot (vide infra) the desired result? (My previous code runs correctly with this file without modification.) I substituted the turbo colormap (new in R2021a) for jet.
T1 = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/807724/URM_shear_EPX1.xlsx', 'VariableNamingRule','preserve');
Extremes = varfun(@(x)[min(x); max(x)], T1);
Extremes.Properties.RowNames = {'Minimum','Maximum'}
Extremes = 2×3 table
Fun_X Coordinate (mm) Fun_Z Coordinate (mm) Fun_Shear Stress (MPa) _____________________ _____________________ ______________________ Minimum -12.533 -25.066 -0.68366 Maximum 0.167 0.334 8.5386
% First10Rows = T1(1:10,:)
% T1Sz = size(T1)
VarNames = T1.Properties.VariableNames;
N = 50; % Interpolation Matrix Size
xv = linspace(min(T1{:,1}), max(T1{:,1}), N); % Create Vector
yv = linspace(min(T1{:,2}), max(T1{:,2}), N); % Create Vector
[Xm,Ym] = ndgrid(xv,yv); % Create Interpolation Matrices
Zm = griddata(T1{:,1}, T1{:,2}, T1{:,3}, Xm, Ym); % Interpolate
Warning: Duplicate data points have been detected and removed - corresponding values have been averaged.
figure
surfc(Xm, Ym, Zm)
grid on
shading interp
set(0, 'DefaultTextInterpreter', 'latex')
hcb = colorbar;
xlabel(hcb,'[{\it Pa}]');
hcb.Label.Interpreter = 'latex';
set(hcb,'TickLabelInterpreter','latex')
colormap(turbo(50))
title ('EPX1','FontSize',13,'interpreter', 'latex')
set(0, 'DefaultTextInterpreter', 'latex')
hcb = colorbar;
hcb.TickLabelInterpreter='latex';
ColorbarRulerProperties = hcb.Ruler;
hcb.Ruler.TickLabelFormat = '%.2f';
% set(hcb, 'Ticks', sort([hcb.Limits, hcb.Ticks]))
%%%%%%%%%%%%
Nticks = 10; % define number of ticks displayed in colorbar
aa = hcb.Limits;
CBAR_ticks = linspace(aa(1),aa(2),Nticks);
set(hcb, 'Ticks', CBAR_ticks);
%%%%%%%%%%%%
set(gca,'TickLabelInterpreter','latex')
% tix = hcb.Ticks;
% expstr = @(x) [x(:).*10.^ceil(-log10(abs(x(:)+(x==0)))) floor(log10(abs(x(:)+(x==0))))];
% tixexp = expstr(tix(:))
% tixexplbl = compose('%.2f \\times 10^{%2d}',[tixexp(:,1) fix(tixexp(:,2))])
% hcb.TickLabels = tixexplbl;
xlabel('{\it x} [{\it mm}]')
ylabel('{\it y} [{\it mm}]')
zlabel('{\it} Shear stress [{MPa}]')
Are there any modifications to be made to the code to produce a different result?
.
Francesco Marchione
Francesco Marchione 2021-11-20
I would like to see in the graph the x values from 0 to -25.40 mm (i.e., I would like to see these extreme values represented), and the y extreme values as well.
I would also like to decide the interval of z values to plot.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Polygons 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by