I have a file excel with x,y coordinates and stresses for z coordinate in order to plot a 3D surface.
How can I get this surface with latex interpreter and colorbar?
I attach the excel file.
Thanks

 Accepted Answer

Try something like this —
T1 = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/616758/Shear%20stress%20adhesive.xlsx', 'VariableNamingRule','preserve');
% First10Rows = T1(1:10,:)
T1Sz = size(T1)
T1Sz = 1×2
69696 3
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
hcb = colorbar;
hcb.TickLabelInterpreter='latex';
xlabel(VarNames{1}, 'Interpreter','latex')
ylabel(VarNames{2}, 'Interpreter','latex')
zlabel(VarNames{3}, 'Interpreter','latex')
Experiment to get different results.
.

7 Comments

Thank you Star.
In the first line:
T1 = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/616758/Shear%20stress%20adhesive.xlsx', 'VariableNamingRule','preserve');
shall I write the path to the file in my pc? For example:
T1 = readtable('C:\Users\MARCHIONE\Desktop\Shear%20stress%20adhesive.xlsx', 'VariableNamingRule','preserve');
Forgive me, but I am a very beginner.
Thanks so much
I tried like this, but it does not work.
"Matlab can only run a file with a valid name"
As always, my pleasure!
Your readtable call should be:
T1 = readtable('Shear stress adhesive.xlsx', 'VariableNamingRule','preserve');
or:
T1 = readtable('C:\Users\MARCHIONE\Desktop\Shear stress adhesive.xlsx', 'VariableNamingRule','preserve');
(or something similar, such as you posted, appropriate to your MATLAB installation), since I assume you are reading it from a directory on your MATLAB path.
(The online Run application here allows us to copy the full file name and run it on MATLAB Answers, so that accounts for the full directory path being part of the file name in the code I posted. That would work if you decided to use my code and run it here, however not on your own computer.)
.
Now I would like to format the colorbar label: I would like to have it in the format 2.00 x 10^4 with every first number with two decimals and the exponential following.
I wrote:
set(hcb,'ColorScale','exp',num2str(tix,'%.1f'))
So, I managed to have the exponent but I could not set the number of decimals.
Thank you so much
We must be using different data, since the magnitude of the z-axis data is nowhere near . Also, the set call does not work in R2021a, so I went property spelunking to find the version that works in R2021a. Those results are part of this Comment.
In any event, I cannot get the tick labels to work as you would like them to. I have no idea what the problem is.
Try this —
T1 = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/616758/Shear%20stress%20adhesive.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
Warning: Duplicate data points have been detected and removed - corresponding values have been averaged.
figure
surfc(Xm, Ym, Zm)
grid on
hcb = colorbar;
hcb.TickLabelInterpreter='latex';
% ColorbarRulerProperties = hcb.Ruler
hcb.Ruler.Exponent = -1;
hcb.Ruler.TickLabelFormat = '%5.2f';
% 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(VarNames{1}, 'Interpreter','latex')
ylabel(VarNames{2}, 'Interpreter','latex')
zlabel(VarNames{3}, 'Interpreter','latex')
Except for ‘ColorbarRulerProperties’ assignment (that displays the appropriate properties), the commented-out lines create the correct tick labels, although the tick labels the code creates (for whatever reason) do not display at all. The result of the compose call are correct. I tried several different formats, however they all refuse to create colorbar ticks.
In the end, I went with what I could get to work.
.
Thank you so much
As always, my pleasure!

Sign in to comment.

More Answers (0)

Categories

Find more on Interpolation in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!