3D plot from excel
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
Francesco Marchione
il 20 Nov 2021
Commentato: Francesco Marchione
il 20 Nov 2021
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
0 Commenti
Risposta accettata
Star Strider
il 20 Nov 2021
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')
Stats = varfun(@(x)[min(x); max(x)], T1)
% 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}]')
.
4 Commenti
Star Strider
il 20 Nov 2021
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'}
% 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(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?
.
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Graphics Object Properties in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!