You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
How to interpolate and plot discrete measurement data corresponding to spacial coordinates?
19 views (last 30 days)
Show older comments
Hi,
I have discrete temperature data (55x1 double) related to spacial coordinates (x,y,z all 55x1 double).
It should show the temperature distribution of a human scalp.
So far i could realize a simple scatter3 plot, where you can see the different temperatures (by color) at their measurement position.
Is it possible to interpolate the temperature from one measurement point to the neighbour points and plot a surface, that depicts the surface of the head (approx.) colored regarding its temperature?
Attached you can find the scatter3 plot and a sample dataset.
Code (scatter3):
load('coordinates_and_temperature.mat');
scatter3(x,y,z,100,T(1:55,1), 'filled');
colorbar;
I allready tried to build something with scatteredInterpolant, but unfortunately I am not able to plot the results.
Code (scatteredInterpolant):
load('coordinates_and_temperature.mat');
[X,Y,Z]=meshgrid(x,y,z);
F=(scatteredInterpolant(x,y,z,T));
V=F(X,Y,Z);
Honestly, I don't know what ist happening in the section above. I think V should be the interpolatet temperature data in x,y,z ?!
If so, how could I plot the interpolatet data only on the approximated surface?
I hope you can help me. If you need any more information don't hesitate to ask.
Kind regards,
Philip
Accepted Answer
Star Strider
on 8 Oct 2022
It is definitely possible to interpolate those values to a finer set of coordinates and to use scatter3 to display them with respect to temperature.
Try something like this —
LD = load(websave('coordinates_and_temperature','https://www.mathworks.com/matlabcentral/answers/uploaded_files/1149095/coordinates_and_temperature.mat'))
x = LD.x;
y = LD.y;
z = LD.z;
T = LD.T;
N = 500;
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);
Tm = griddata(x,y,T,Xm,Ym);
figure
scatter3(Xm(:),Ym(:),Zm(:), 10, Tm(:))
colormap(turbo(N))
grid on
hcb = colorbar;
tix = hcb.Ticks;
hcb.TickLabels = compose('%5.1f°C',tix);
hcb.Label.String = 'Temperature';
xlabel('X')
ylabel('Y')
zlabel('Z')
This uses two separate griddata calls, one to interplate the surface values and another to interpolate the temperatures, and adds the colorbar. .
.
12 Comments
PG
on 8 Oct 2022
Hello Star Strider,
thank you very much for your blazing fast reply, this helped a lot. Fantastic!
A few more questions for understanding and visual improvements.
The command Zm=griddata(...) takes my spacial measurement points and interpolates further coordinates regarding the new formed N by N grids (Xm and Ym). Is my assumption right?
And is it because my spacial points are not strictly descending or ascending, but for example at the centerline (values x(1:9,1)) where the last x value (x(9,1)) is smaller than than the value before (x(8)), that I am getting these notches and wide "meshing", shown below? Would i get a closed and smoother looking edge if the x and y values were strictly ascending or descending?
Or is there a better way to do so? Upscaling N did help to close the wide "meshing" but the notches at the very back of the figure are still remaining.
I hope my formulation is clearly enough.
Thanks you very much!
Kind Regards,
Philip
LD = load(websave('coordinates_and_temperature','https://www.mathworks.com/matlabcentral/answers/uploaded_files/1149095/coordinates_and_temperature.mat'))
LD = struct with fields:
T: [55×1 double]
x: [55×1 double]
y: [55×1 double]
z: [55×1 double]
x = LD.x;
y = LD.y;
z = LD.z;
T = LD.T;
for i=1:2
N = 500*i;
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);
Tm = griddata(x,y,T,Xm,Ym);
figure(i)
scatter3(Xm(:),Ym(:),Zm(:), 10, Tm(:), 'filled')
hold on
plot3(x,y,z+1, '.', 'color', 'k')
colormap(turbo(N))
grid on
title('N=', N)
set(gca, 'CameraPosition', [675.649063658727,-1551.934893838251,511.3003837257489]);
hcb = colorbar;
tix = hcb.Ticks;
hcb.TickLabels = compose('%5.1f°C',tix);
hcb.Label.String = 'Temperature';
xlabel('X')
ylabel('Y')
zlabel('Z')
end
Star Strider
on 8 Oct 2022
My pleasure!
‘The command Zm=griddata(...) takes my spacial measurement points and interpolates further coordinates regarding the new formed N by N grids (Xm and Ym). Is my assumption right?’
Yes.
‘And is it because my spacial points are not strictly descending or ascending, but for example at the centerline (values x(1:9,1)) where the last x value (x(9,1)) is smaller than than the value before (x(8)), that I am getting these notches and wide "meshing", shown below? Would i get a closed and smoother looking edge if the x and y values were strictly ascending or descending?’
I am not certain because I did not look at the original data. I created ‘xv’ and ‘yv’ so the resulting matrices are regular and provide a regular interpolation surface.
I did not see the gaps, however I was not looking for them. They are likely due to the fact that therre are no data in those regions to interpolate. The griddata function does not extrapolate, and I generally discourage extrapolation because it creates data that do not exist in the original data set.
The scatteredInterpolant function can extrapolate, and while I caution against it, feel free to experiment with extrapolation.
Example —
LD = load(websave('coordinates_and_temperature','https://www.mathworks.com/matlabcentral/answers/uploaded_files/1149095/coordinates_and_temperature.mat'))
LD = struct with fields:
T: [55×1 double]
x: [55×1 double]
y: [55×1 double]
z: [55×1 double]
x = LD.x;
y = LD.y;
z = LD.z;
T = LD.T;
Fz = scatteredInterpolant(x,y,z); % Surface Interpolation
Fz.Method = 'natural';
Fz.ExtrapolationMethod = 'linear';
FT = scatteredInterpolant(x,y,T); % Temperature Interpolation
FT.Method = 'natural';
FT.ExtrapolationMethod = 'linear';
N = 500 * 2;
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);
% Tm = griddata(x,y,T,Xm,Ym);
Zm = Fz(Xm,Ym);
Tm = FT(Xm,Ym);
figure
scatter3(Xm(:),Ym(:),Zm(:), 10, Tm(:))
hold on
plot3(x,y,z+1, '.', 'color', 'k')
hold off
colormap(turbo(N))
grid on
hcb = colorbar;
tix = hcb.Ticks;
hcb.TickLabels = compose('%5.1f°C',tix);
hcb.Label.String = 'Temperature';
xlabel('X')
ylabel('Y')
zlabel('Z')
figure
scatter3(Xm(:),Ym(:),Zm(:), 10, Tm(:))
hold on
plot3(x,y,z+1, '.', 'color', 'k')
hold off
colormap(turbo(N))
grid on
hcb = colorbar;
tix = hcb.Ticks;
hcb.TickLabels = compose('%5.1f°C',tix);
hcb.Label.String = 'Temperature';
xlabel('X')
ylabel('Y')
zlabel('Z')
set(gca, 'CameraPosition', [675.649063658727,-1551.934893838251,511.3003837257489]);
I defer to you to determine how appropriate that result is.
Experiment with the different Method and ExtrapolationMethod options to get the desired result.
.
PG
on 8 Oct 2022
Edited: PG
on 8 Oct 2022
Hello Star Strider,
extrapolation is indeed not the way i want to go with.
For better understanding I included numbering of the order of the discrete measurement ponints in the plot below.
Regarding the density of the meshing, i was wondering why there are spots with lower (triangle [49, 52, 55]) and spots with higher (triangle [37, 38, 46]) mesh-density. And the gaps i was thinking about, why does the code do not interpolate within certain triangles such as triangle [41, 84, 49] and others?
When i was writing about the closing smooth edge the post before, i thought about the possible edge formed by the points [42, 53, 54, 55, 49, 41, 9, 18, 26,... ]. Shouldn't it be possible to interpolate there as well?
As a final result i would like to get a side view according to the graphic you can find attached.
Thank you again for your help.
Best,
Philip
LD = load(websave('coordinates_and_temperature','https://www.mathworks.com/matlabcentral/answers/uploaded_files/1149095/coordinates_and_temperature.mat'))
LD = struct with fields:
T: [55×1 double]
x: [55×1 double]
y: [55×1 double]
z: [55×1 double]
x = LD.x;
y = LD.y;
z = LD.z;
T = LD.T;
Num=[1:55]';
Num=num2str(Num);
N = 500;
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);
Tm = griddata(x,y,T,Xm,Ym);
figure
scatter3(Xm(:),Ym(:),Zm(:), .3, Tm(:), 'filled')
hold on
text(x, y-7, z+2, Num, 'FontSize', 8, 'Color', 'k'); %remove offset from y for top view
colormap(turbo(N))
grid on
title('N=', N)
set(gca, 'CameraPosition',[789.2471350145108,-554.391906023069,736.527638196491], 'View', [43.4736328125,54.5146484375]);
hcb = colorbar;
tix = hcb.Ticks;
hcb.TickLabels = compose('%5.1f°C',tix);
hcb.Label.String = 'Temperature';
xlabel('X')
ylabel('Y')
zlabel('Z')
Star Strider
on 8 Oct 2022
I am lost. Increasing ‘N’ will increase the number of interpolated points, and that may be the best option. The scatteredInterpolant function will extrapolate, however I am not certain how to control for that because I have no idea what you want extrapolated.
PG
on 10 Oct 2022
Hello Star Strider,
again, I cannot thank you enough for your help. And I marked your first answer as the solution to my question.
I think my last formulation was a little confusing, thus for completion I will share my experiences gained by playing a little with the input data.
By editing a little bit the coordinates of the measurement points I figured out, that the missing interpolation within e.g. area F4 of the attached graphic (scalp_side), results from P2 being further out of the plane than P4 and thus the area F4 being spacially underneath the projectet area to the x-y-plane of the rest of the scalp. I really cannot explain better in english since its not my mother language.
Anyways, the density of interpolation points within a certain surface area such as F3, F2, an F1 results from its angle relative to the x-y-plane. Logically (though I didn't think about it before), an area perpenticular to the x-y-plane will only form interpolation points along the upper edge of the considered area (e.g., with larger z coordinates). Therefore, an interpolation area spun by data of 4 measurement points, which is parallel to the x-y-plane, will have the most ammount of interpolaiton data.
Below you can find a modified code with different temperature data and rearranges order of points. The rearrangement of the temperature and points was only necessary for "rapid prototyping" of the code and is not really necessary for the principles I referred to.
I hope that this thread will help others as well.
Cheers,
Philip
load('coordinates_and_temperature_2');
% Editing spacial data as described
x(7:11)=x(7:11)+4;
y(7:8)=y(7:8)+2;
y(10:11)=y(10:11)-2;
y(5:6)=y(5:6)+5;
y(12:13)=y(12:13)-5;
%
Num=num2str([1:55]');
N = 500;
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);
Tm = griddata(x,y,T,Xm,Ym);
figure(1)
scatter3(Xm(:),Ym(:),Zm(:), 10, Tm(:), 'filled')
hold on
%plot3(x,y,z+1, '.', 'color', 'k') %add to see measurement points
%text(x, y-4, z+2, Num, 'FontSize', 8, 'Color', 'k'); %add to see measurement point order
colormap(turbo(N))
grid on
title('N=', N)
%set(gca, 'CameraPosition',[675.649063658727,-1551.934893838251,511.3003837257489]); %%TOP-View
set(gca, 'CameraPosition',[789.2471350145108,-554.391906023069,736.527638196491], 'View', [43.4736328125,54.5146484375]);
hcb = colorbar;
tix = hcb.Ticks;
hcb.TickLabels = compose('%5.1f°C',tix);
hcb.Label.String = 'Temperature';
xlabel('X')
ylabel('Y')
zlabel('Z')
figure(2)
image(imread('Scalp_Side_2.png'))
set(gca,'xtick',[])
set(gca,'ytick',[])
title('scalp side')
Star Strider
on 10 Oct 2022
As always, my pleasure!
I am only vaguely aware of what you are doing, since while I understand the essence of what you are doing, my knowledge of the details is lacking. I am happy that I have been able to help you get a specific result.
PG
on 5 Apr 2023
Hello Star Strider,
after a while of resting this topic I found some issues with the way of interpolation discussed in this thread.
Quick reminder of the boundary conditions: I have spacial coordinates (x,y,z) which depict a human head. Related to these coordinates I have some temperature (T) data. The goal is to achieve a 3D Plot of the heads surface, colored regarding its local temperature. Since I only have 55 discrete measurement points I need to interpolate. Below you can find plots with code of two different ways of interpolation.
[Interpolation 1]
You allredy helped me to find a solution where T is interpolated over x and y. In some cases this aproach leads to (in my view) unprecisions eg. betwen 29 and 25 i would expect a more "orange-ish" color gradient:
load('Temp_Plot.mat');
N = 800;
xv = linspace(min(x), max(x), N);
yv = linspace(min(y), max(y), N);
[Xm,Ym] = ndgrid(xv, yv);
Xm = griddata(x,y,x,Xm,Ym,'natural'); %For Interpolation 1&2
Ym = griddata(x,y,y,Xm,Ym,'natural'); %For Interpolation 1&2
Zm = griddata(x,y,z,Xm,Ym,'natural'); %For Interpolation 2
Tm = griddata(x,y,T,Xm,Ym,'natural'); %Interpolation 1 in this case T is only interpolated over x and y.
%Tm=griddata(x,y,z,T,Xm,Ym,Zm, 'natural'); %Interpolation 2 in this case T is interpolatet over x,y and z.
%% Plot
figure
scatter3(Xm(:),Ym(:),Zm(:), 10, Tm(:))
colormap(turbo(N))
view(180,0);
grid on
hcb = colorbar;
tix = hcb.Ticks;
hcb.TickLabels = compose('%5.1f°C',tix);
hcb.Label.String = 'Temperature';
xlabel('X')
ylabel('Y')
zlabel('Z')
hold on
% Show sample points
y_text=y;
y_text(26:32,1)=y_text(26:32,1)+1;
plot3(x(1:32), y_text(1:32)+5, z(1:32), '.', 'color', 'k', 'MarkerSize', 4)
% Show numbering of sample points
Num=num2str([1:32]');
text(x(1:32)+5, y(1:32)+5, z(1:32)+3, Num, 'FontSize', 8, 'Color', 'k');
%Top View
figure
scatter3(Xm(:),Ym(:),Zm(:), 10, Tm(:))
colormap(turbo(N))
view(180,90);
grid on
hcb = colorbar;
tix = hcb.Ticks;
hcb.TickLabels = compose('%5.1f°C',tix);
hcb.Label.String = 'Temperature';
xlabel('X')
ylabel('Y')
zlabel('Z')
hold on
% Show sample points
y_text=y;
y_text(26:32,1)=y_text(26:32,1);
plot3(x(1:32)+3, y_text(1:32), z(1:32)+20, '.', 'color', 'k', 'MarkerSize', 4)
% Show numbering of sample points
Num=num2str([1:32]');
text(x(1:32)+7, y(1:32)-5, z(1:32)+50, Num, 'FontSize', 8, 'Color', 'k');
I think this relates to the fact that T is only interpolated over x and y. Thus in top view (2D) point(24) is closer to point(26) than to point(29).
[Interpolation 2]
By experimenting a little with griddata I found another solution, where (in my view) the interpolation seems to be more apropriate:
load('Temp_Plot.mat');
N = 800;
xv = linspace(min(x), max(x), N);
yv = linspace(min(y), max(y), N);
[Xm,Ym] = ndgrid(xv, yv);
Xm = griddata(x,y,x,Xm,Ym,'natural'); %For Interpolation 1&2
Ym = griddata(x,y,y,Xm,Ym,'natural'); %For Interpolation 1&2
Zm = griddata(x,y,z,Xm,Ym,'natural'); %For Interpolation 2
%Tm = griddata(x,y,T,Xm,Ym,'natural'); %Interpolation 1 in this case T is only interpolated over x and y.
Tm=griddata(x,y,z,T,Xm,Ym,Zm, 'natural'); %Interpolation 2 in this case T is interpolatet over x,y and z.
%% Plot
figure
scatter3(Xm(:),Ym(:),Zm(:), 10, Tm(:))
colormap(turbo(N))
view(180,0);
grid on
hcb = colorbar;
tix = hcb.Ticks;
hcb.TickLabels = compose('%5.1f°C',tix);
hcb.Label.String = 'Temperature';
xlabel('X')
ylabel('Y')
zlabel('Z')
hold on
% Show sample points
y_text=y;
y_text(26:32,1)=y_text(26:32,1)+1;
plot3(x(1:32), y_text(1:32)+5, z(1:32), '.', 'color', 'k', 'MarkerSize', 4)
% Show numbering of sample points
Num=num2str([1:32]');
text(x(1:32)+5, y(1:32)+5, z(1:32)+3, Num, 'FontSize', 8, 'Color', 'k');
Unforunately there are some triangled gaps with unexpected color paterns and less interpolation points.
Any Ideas how to handle this better?
Thanks in advance,
Philip
Star Strider
on 18 Apr 2023
I’m not ignoring you. I had to go back and remember what we were doing here. It’s been a while!
Most recently, I’ve decided that the scatteredInterpolant function (as opposed to any gridded interpolation unless gridded interpolation is required) is significantly superior for these sorts of problems. (It also has definite advantages with respect to drawing lines on surfaces, if that becomes necessary.) So I experimented with it here.
Also, the interpolated ‘Xm’ and ‘Ym’ were over-writing the original matrices by those names generated by the ndgrid call, so I re-named the ndgrid outputs to ‘Xa’ and ‘Ya’ and changed the subsequent code to reflect that. I’m not certaiin if this was causing problems, however good programming practice is to not overwrite existing variables without good reason.
Using griddedInterpolant here instead of griddata, and simply substituting the interpolation calls for the griddata calls, Interpolation 1 becomes —
load('Temp_Plot.mat')
whos % Show Loaded Variables
Name Size Bytes Class Attributes
Num 32x2 128 char
T 55x1 440 double
ans 1x38 76 char
cmdout 1x33 66 char
x 55x1 440 double
y 55x1 440 double
y_text 55x1 440 double
z 55x1 440 double
N = 800;
xv = linspace(min(x), max(x), N);
yv = linspace(min(y), max(y), N);
[Xa,Ya] = ndgrid(xv, yv);
% Xm = griddata(x,y,x,Xm,Ym,'natural'); %For Interpolation 1&2
% Ym = griddata(x,y,y,Xm,Ym,'natural'); %For Interpolation 1&2
% Zm = griddata(x,y,z,Xm,Ym,'natural'); %For Interpolation 2
% Tm = griddata(x,y,T,Xm,Ym,'natural'); %Interpolation 1 in this case T is only interpolated over x and y.
% %Tm=griddata(x,y,z,T,Xm,Ym,Zm, 'natural'); %Interpolation 2 in this case T is interpolatet over x,y and z.
Fx = scatteredInterpolant(x,y,x, 'natural', 'none');
Fy = scatteredInterpolant(x,y,y, 'natural', 'none');
Fz = scatteredInterpolant(x,y,z, 'natural', 'none');
FT = scatteredInterpolant(x,y,T, 'natural', 'none');
Xm = Fx(Xa,Ya);
Ym = Fy(Xa,Ya);
Zm = Fz(Xa,Ya);
Tm = FT(Xa,Ya);
%% Plot
figure
scatter3(Xm(:),Ym(:),Zm(:), 10, Tm(:))
colormap(turbo(N))
view(180,0);
grid on
hcb = colorbar;
tix = hcb.Ticks;
hcb.TickLabels = compose('%5.1f°C',tix);
hcb.Label.String = 'Temperature';
xlabel('X')
ylabel('Y')
zlabel('Z')
hold on
% Show sample points
y_text=y;
y_text(26:32,1)=y_text(26:32,1)+1;
plot3(x(1:32), y_text(1:32)+5, z(1:32), '.', 'color', 'k', 'MarkerSize', 4)
% Show numbering of sample points
Num=num2str([1:32]');
text(x(1:32)+5, y(1:32)+5, z(1:32)+3, Num, 'FontSize', 8, 'Color', 'k');
%Top View
figure
scatter3(Xm(:),Ym(:),Zm(:), 10, Tm(:))
colormap(turbo(N))
view(180,90);
grid on
hcb = colorbar;
tix = hcb.Ticks;
hcb.TickLabels = compose('%5.1f°C',tix);
hcb.Label.String = 'Temperature';
xlabel('X')
ylabel('Y')
zlabel('Z')
hold on
% Show sample points
y_text=y;
y_text(26:32,1)=y_text(26:32,1);
plot3(x(1:32)+3, y_text(1:32), z(1:32)+20, '.', 'color', 'k', 'MarkerSize', 4)
% Show numbering of sample points
Num=num2str([1:32]');
text(x(1:32)+7, y(1:32)-5, z(1:32)+50, Num, 'FontSize', 8, 'Color', 'k');
And Interpolation 2 becomes —
clearvars
load('Temp_Plot.mat');
whos % Show Loaded Variables
Name Size Bytes Class Attributes
Num 32x2 128 char
T 55x1 440 double
x 55x1 440 double
y 55x1 440 double
y_text 55x1 440 double
z 55x1 440 double
N = 800;
xv = linspace(min(x), max(x), N);
yv = linspace(min(y), max(y), N);
[Xa,Ya] = ndgrid(xv, yv);
% Xm = griddata(x,y,x,Xm,Ym,'natural'); %For Interpolation 1&2
% Ym = griddata(x,y,y,Xm,Ym,'natural'); %For Interpolation 1&2
% Zm = griddata(x,y,z,Xm,Ym,'natural'); %For Interpolation 2
%
% %Tm = griddata(x,y,T,Xm,Ym,'natural'); %Interpolation 1 in this case T is only interpolated over x and y.
% Tm=griddata(x,y,z,T,Xm,Ym,Zm, 'natural'); %Interpolation 2 in this case T is interpolatet over x,y and z.
Fx = scatteredInterpolant(x,y,x, 'natural', 'none');
Fy = scatteredInterpolant(x,y,y, 'natural', 'none');
Fz = scatteredInterpolant(x,y,z, 'natural', 'none');
FT = scatteredInterpolant(x,y,T, 'natural', 'none');
Xm = Fx(Xa,Ya);
Ym = Fy(Xa,Ya);
Zm = Fz(Xa,Ya);
Tm = FT(Xa,Ya);
%% Plot
figure
scatter3(Xm(:),Ym(:),Zm(:), 10, Tm(:))
colormap(turbo(N))
view(180,0);
grid on
hcb = colorbar;
tix = hcb.Ticks;
hcb.TickLabels = compose('%5.1f°C',tix);
hcb.Label.String = 'Temperature';
xlabel('X')
ylabel('Y')
zlabel('Z')
hold on
% Show sample points
y_text=y;
y_text(26:32,1)=y_text(26:32,1)+1;
plot3(x(1:32), y_text(1:32)+5, z(1:32), '.', 'color', 'k', 'MarkerSize', 4)
% Show numbering of sample points
Num=num2str([1:32]');
text(x(1:32)+5, y(1:32)+5, z(1:32)+3, Num, 'FontSize', 8, 'Color', 'k');
That seems to me to be a much more favourable result than the gridded approach provided. Most notably, the artefacts no longer appear to be present.
.
PG
on 19 Apr 2023
Edited: PG
on 19 Apr 2023
Hello,
thanks for your effort! As far as I am concerned there is no difference betwen your Interpolation 1 and 2, since in both cases T is only interpolated regarding x and y coordinates. Thus there is no difference between my Interpolation 1 with griddata and your aproaches, however i agree overwriting variables should be avoided.
I found it problematically to interpolate as described, since in 2D point(24) and point(26) are closer to another than point(25) and point(29). But in 3D-View, point(26) is way closer to point(29) and thus i think the inperpolated pattern should look as I described before.
By changing your script for interpolation 2, Tm being interpolated regarding x,y and z, those artefacts still occure. However the Interpolation between looks somehow better to me.
Any Ideas how to homogenize the results?
load('Temp_Plot.mat');
N = 800;
xv = linspace(min(x), max(x), N);
yv = linspace(min(y), max(y), N);
[Xa,Ya] = ndgrid(xv, yv);
% Xm = griddata(x,y,x,Xm,Ym,'natural'); %For Interpolation 1&2
% Ym = griddata(x,y,y,Xm,Ym,'natural'); %For Interpolation 1&2
% Zm = griddata(x,y,z,Xm,Ym,'natural'); %For Interpolation 2
%
% %Tm = griddata(x,y,T,Xm,Ym,'natural'); %Interpolation 1 in this case T is only interpolated over x and y.
% Tm=griddata(x,y,z,T,Xm,Ym,Zm, 'natural'); %Interpolation 2 in this case T is interpolatet over x,y and z.
Fx = scatteredInterpolant(x,y,x, 'natural', 'none');
Fy = scatteredInterpolant(x,y,y, 'natural', 'none');
Fz = scatteredInterpolant(x,y,z, 'natural', 'none');
FT = scatteredInterpolant(x,y,z,T, 'natural', 'none');
Xm = Fx(Xa,Ya);
Ym = Fy(Xa,Ya);
Zm = Fz(Xa,Ya);
Tm = FT(Xa,Ya,Zm);
%% Plot
figure
scatter3(Xm(:),Ym(:),Zm(:), 10, Tm(:))
colormap(turbo(N))
view(180,0);
grid on
hcb = colorbar;
tix = hcb.Ticks;
hcb.TickLabels = compose('%5.1f°C',tix);
hcb.Label.String = 'Temperature';
xlabel('X')
ylabel('Y')
zlabel('Z')
hold on
% Show sample points
y_text=y;
y_text(26:32,1)=y_text(26:32,1)+1;
plot3(x(1:32), y_text(1:32)+5, z(1:32), '.', 'color', 'k', 'MarkerSize', 4)
% Show numbering of sample points
Num=num2str([1:32]');
text(x(1:32)+5, y(1:32)+5, z(1:32)+3, Num, 'FontSize', 8, 'Color', 'k');
Star Strider
on 19 Apr 2023
I was in the process of commenting on this a couple hours ago when Win 11 crashed. Again. I really hate Micro$oft at times like this.
One problem may be in these assignments in your code (that I do not understand, in the context of the code):
FT = scatteredInterpolant(x,y,z,T, 'natural', 'none');
and
Tm = FT(Xa,Ya,Zm);
since the first creates a much different interpolant as described in the documentation section on 3-D Interpolation and the second uses an interpolated matrix ‘Zm’ for the third argument. That may be the problem.
My code uses:
FT = scatteredInterpolant(x,y,T, 'natural', 'none');
and
Tm = FT(Xa,Ya);
to create the same matrices. However they are not actually the same matrices. Your code creates a matrix with 149428 NaN values, and my code 146650 NaN values.
Running your most recent code and file here gives the same result you posted (using the ‘Temp_Plot.mat’ file uploaded a few hours ago) while running my code gives the same result I posted.
I guess my problem is that I simply do not understand the reason that you create ‘FT’ and ‘Tm’ as you do in your code, since it simply seems inappropriate to me, and also does not give the desired result.
My ‘fix’ for it is to use my ‘FT’ and ‘Tm’ interpolations, since they appear to create the appropriate matrices, and do not display the artefacts. That is the best I can do.
.
PG
on 9 May 2023
I apologize for the delay in my response.
I still have concerns about your approach, specifically with regards to the interpolation of T over only x and y, despite its dependence on x, y, and z. While the resulting interpolation may appear visually satisfying due to its completeness, it may lead to an incorrect distribution. In reality, this problem seems to be more of a 4D case rather than a 3D one.
Once again, I appreciate your effort, and unless you have any further advice or ideas, I think we can conclude our discussion.
Best regards
Star Strider
on 9 May 2023
As always, my pleasure!
No worries on the delay.
Part of the problem is that I likely do not completely understand what you want to do, or the constraints of the depiction of the data, especially ‘Tm’, since one of the matrices used to create it is ‘Zm’ and ‘Zm’ is itself interpolated. That information may be difficult to describe, however it appears to me that using an interpolated matrix as an interpolant may cause the artefacts in the resulting image, those artefacts being the problem. I am not certain that there is a solution to that.
Creating ‘Tm’ without using ‘Zm’ produces an artefact-free result, however that apparently is not the desired result for reasons that I do not understand. I doubt that creating an artefact-free result that meets your other requirements is possible. I wish that it was.
More Answers (0)
See Also
Categories
Find more on 2-D and 3-D Plots 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!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)