1

I am trying to create a plot in Matlab with my 4 dimensional data. the data looks like [x,y,z,d] in which x,y and z are the coordinates of the object and "d" is the intensity. I tried to map the data and consider just [x,y,d] and do contour or surf but for that "d" should be a matrix. I also used plot3 but it does not look nice or like a contour. Any idea how to plot this in a nice way? Here is an example of the data:

  1.2500000E-02  1.2500000E-02  1.2500000E-02  1.054474998136970E-002
  1.2500000E-02  1.2500000E-02  3.7500001E-02  1.348833743199940E-003
  1.2500000E-02  1.2500000E-02  6.2500000E-02  0.000000000000000E+000
  1.2500000E-02  1.2500000E-02  8.7499999E-02  0.000000000000000E+000
  1.2500000E-02  1.2500000E-02  0.1125000      0.000000000000000E+000
  1.2500000E-02  1.2500000E-02  0.1375000      0.000000000000000E+000
  1.2500000E-02  1.2500000E-02  0.1625000      0.000000000000000E+000
  1.2500000E-02  1.2500000E-02  0.1875000      0.000000000000000E+000
  1.2500000E-02  1.2500000E-02  0.2125000      0.000000000000000E+000
  1.2500000E-02  1.2500000E-02  0.2375000      0.000000000000000E+000
  1.2500000E-02  1.2500000E-02  0.2625000      0.000000000000000E+000
  1.2500000E-02  1.2500000E-02  0.2875000      0.000000000000000E+000
  1.2500000E-02  1.2500000E-02  0.3125000      0.000000000000000E+000
  1.2500000E-02  1.2500000E-02  0.3375000      0.000000000000000E+000
  1.2500000E-02  1.2500000E-02  0.3625000      0.000000000000000E+000
  1.2500000E-02  1.2500000E-02  0.3875000      0.000000000000000E+000
  1.2500000E-02  1.2500000E-02  0.4125000      0.000000000000000E+000
  1.2500000E-02  1.2500000E-02  0.4375000      7.452120128176466E-006
  1.2500000E-02  1.2500000E-02  0.4625000      2.235636038452940E-005
  1.2500000E-02  1.2500000E-02  0.4875000      4.471272076905880E-005
  1.2500000E-02  1.2500000E-02  0.5125000      0.000000000000000E+000
  1.2500000E-02  1.2500000E-02  0.5375000      0.000000000000000E+000
  1.2500000E-02  1.2500000E-02  0.5625000      0.000000000000000E+000
  1.2500000E-02  1.2500000E-02  0.5875000      0.000000000000000E+000
  1.2500000E-02  1.2500000E-02  0.6125000      0.000000000000000E+000
  1.2500000E-02  1.2500000E-02  0.6375000      0.000000000000000E+000
  1.2500000E-02  1.2500000E-02  0.6625000      0.000000000000000E+000
  1.2500000E-02  1.2500000E-02  0.6875000      0.000000000000000E+000
  1.2500000E-02  1.2500000E-02  0.7125000      0.000000000000000E+000
  1.2500000E-02  1.2500000E-02  0.7375000      0.000000000000000E+000
  1.2500000E-02  1.2500000E-02  0.7625000      0.000000000000000E+000
  1.2500000E-02  1.2500000E-02  0.7875000      0.000000000000000E+000
  1.2500000E-02  1.2500000E-02  0.8125000      0.000000000000000E+000
  1.2500000E-02  1.2500000E-02  0.8375000      0.000000000000000E+000
  1.2500000E-02  1.2500000E-02  0.8625000      0.000000000000000E+000
  1.2500000E-02  1.2500000E-02  0.8875000      0.000000000000000E+000
  1.2500000E-02  1.2500000E-02  0.9125000      0.000000000000000E+000
  1.2500000E-02  1.2500000E-02  0.9375000      7.824726134585290E-004
  1.2500000E-02  1.2500000E-02  0.9625000      9.657947686116700E-003
  1.2500000E-02  1.2500000E-02  0.9875000      3.128400029808481E-002
  1.2500000E-02  3.7500001E-02  1.2500000E-02  1.028392577688352E-003

Thanks

Elin
  • 61
  • 1
  • 5
  • Maybe the answer is there: http://stackoverflow.com/questions/28403655/drawing-3d-contour-plot-from-3d-vector/35510265#35510265 – Crowley Mar 07 '16 at 17:53
  • 1
    Can You describe your plot as a function `(z,d)=f(x,y)` or as `d=f(x,y,z)? In the first case you can use surf where `z` defines elevation and `d` describes colour. In the second case You have to chose a slice `z=f(x,y)` and use it to get `[z,d]=g(x,y)`. This I can advise you right now. – Crowley Mar 07 '16 at 17:58
  • What dimension are your variables? Can you append the code you used for `plot` and the image? It might help understanding the data. – Daniel Mar 07 '16 at 18:04
  • Your pasted data does not look like it actually defines a surface, for the same x and y value you have multiple z values. Does this describe a volume? – Daniel Mar 07 '16 at 18:05
  • @Daniel That means, that the data represents, say, temperature in a volume. – Crowley Mar 07 '16 at 18:19

1 Answers1

0

Suppose we CAN transform your matrix in form [x,y,z,T] to form 3 vectors X, Y, Z and 3-D matrix T while T(ii,jj,kk) corresponds to X(ii), Y(jj) and Z(kk).

It is impossible to plot such field as it is, data visualized closest to the viewer will hide the rest, we can plot a subset of such field, let's call it slice although it doesn't meat it must be a plane.

In my code suppose the slice being defined as a function z=f(x,y).

In the first part I define X, Y, Z vectors and field T which intensity is equal to distance to [0 0 0] position.

Then I create slice and using encapsulated function getTvalue I calculate appropriate intesity.

getTvalue interpolates T(z) dependence from the mesh and calculate the value for given Z.

Then I plot the slice using surf to show the geometry (first 3 parameters) the fourth represents the colour - which is, in our case, intensity.

function[]=plot4d()
clc,close all
% Define the mesh
N=21;
X=-1:1/(N-1):1;
Y=X;
Z=X;
M=size(X,2);

% Define intensity matrix
T=zeros(M,M,M);
for ii=1:M
  for jj=1:M
    for kk=1:M
      T(ii,jj,kk)=sqrt((X(ii))^2+(Y(jj))^2+(Z(kk))^2);
    end
  end
end

for ii=1:M
  for jj=1:M
    SliceZ(ii,jj)=X(ii)+(Y(jj)^2);    % This is the SLICE FUNCTION, here (z=x+y^2)
% Calculate T value for (ii,jj) position using Z mesh, T values and actual Z position)
    SliceT(ii,jj)=getTvalue(ii,jj,Z,T,SliceZ(ii,jj));
  end
end

% Plot surface in range of X and Y, elevation defined by SliceZ and colour defined by SliceT without lines
surf(X,Y,SliceZ,SliceT,'linestyle','none')
% Plot the cross locating [0 0 0] point
line('xdata',[-1 1],'ydata',[0 0],'zdata',[0 0])
line('ydata',[-1 1],'xdata',[0 0],'zdata',[0 0])
line('zdata',[-1 1],'ydata',[0 0],'xdata',[0 0])
axis equal
% plot4d ends here

%% Nested functions:

function[Tvalue]=getTvalue(xi,yj,Zvector,Tmatrix,Zvalue)
% Test if the actual slice position (Zvalue) is inside the mesh (Zvector)
if Zvalue>max(Zvector)||Zvalue<min(Zvector)
  % If not, return NaN and process to next iteration
  Tvalue=nan;
  return
end

M=max(size(Zvector));                      % get the size of Z mesh
Index=find(Zvalue<=Zvector,1)              % find interval inside which the Zvalue is

% Test if the Zvalue is on the edge of mesh (Zvector)
if Index==M
  % Zvalue is on the edge - return the edge value (it prevents the index overflow)
  Tvalue=Tmatrix(xi,yj,M);
else
% Zvalue is inside the mesh (Zvector)

% calculate the slope of T(x,y,z)=k*z(x,y)+q dependence.
k=(Tmatrix(xi,yj,Index)-Tmatrix(xi,yj,Index+1))/(Zvector(Index)-Zvector(Index+1));

% calculate transposition of the dependence   
q=Tmatrix(xi,yj,Index)-k*(Zvector(Index)); 

% calculate the actual value of T
Tvalue=k*Zvalue+q; 
end
% End of nested function
Crowley
  • 2,279
  • 6
  • 22
  • 36
  • Answer is open to any edit of both code and/or explanation. – Crowley Mar 07 '16 at 20:22
  • Actually, since `X` and `Y` are "functions" of indices `ii` and `jj` You can tune the function to have the *slice* as a ball, torus or any other shape You can define by vectors `X(ii)`, `Y(jj)` and matrix `Z(ii,jj)`. In other words, the slice must be defined by function `f(x,y,z)=0` and it is up to You how You define the `X`, `Y`, `Z` and `T`. – Crowley Mar 07 '16 at 20:48