4D plot - Radiation pattern
Show older comments
Dear Community,
I have to create a 4D Plot. It has to be something like this: https://www.mathworks.com/matlabcentral/mlc-downloads/downloads/submissions/9519/versions/9/screenshot.jpg
I created an matrix in x, y direction and also in z direction- This 3 are my frist values. The fourth values is the quantity of reads per point in the matrix.
I tried to create an code but I failed. The Problem is that the z value is only in 3 steps not in 21 steps like the other x and y direction.
You can find my code in the appendix.
My commande window on matlab tells me that ther is a issue with the z data: *Error using surf (line 82) Z must be a matrix, not a scalar or vector.
Error in test (line 80) surf(x,y,z,values) *
I would be very happy if you can help me to solve my problem
Reagards Süleyman
Answers (1)
Cam Salzberger
on 15 May 2017
Edited: Cam Salzberger
on 15 May 2017
1 vote
Hello Süleyman,
I think there's a slight misunderstanding happening here. "surf" doesn't quite produce a 4-D plot. It's a 3-D surface, with the possibility of customizing the color of that surface. From my understanding of your code, it looks like you have three separate planes in the z-dimension, each of which has values which you would like to display as colors.
For this, I would recommend three separate calls to "surf", one for each plane, with the corresponding layer of "values". You'll need to specify the "Z" input to surf as a matrix of equal size to one of the layers of "values", but all the same value of "z", to make this flat plane.
Also, I believe that the "values" matrix definition will need to be changed. Currently it comes out as 63x21, when I think you want 21x21x3.
Alternatively, you could just "meshgrid" the x, y, and z values, then plot colored points with "plot3".
-Cam
5 Comments
Süleyman Köken
on 15 May 2017
Cam Salzberger
on 15 May 2017
So the separating out the layers of "values" is simple. If you're defining them all in code, just do something like:
values = cat(3,[1 2 ; 3 4],[5 6 ; 7 8],[9 10 ; 11 12]);
Except with your 21x21 matrices as each input, rather than my example ones there.
Then just loop through each layer and plot the surface with something like:
figure
axes
hold on
for k = 1:size(values,3)
surf(x,y,z(k)*ones(size(values,1),size(values,2)),values(:,:,k))
end
I haven't tested that code, but I think it should be close. The 21x21x21 matrix, assuming you want the same kind of plot, should probably work the same way.
Also, if you want the surface to look more similar to the example you linked, you can just set the EdgeColor and FaceColor properties of the surface plot:
surf(...,'EdgeColor','none','FaceColor','interp')
Süleyman Köken
on 18 May 2017
Cam Salzberger
on 18 May 2017
When you put the code into the Editor, always check any of the Code Analyzer warnings or errors. This will give you clues as to what's going on.
Now I'm not sure if it's the whole going through MATLAB Answers thing, but you can't have that many spaces when creating matrices. Newlines, at the very least, do matter in MATLAB. So instead of:
cat(3,
[
3 4 ;
5 6 ;
]
,
[
blah blah...
Try putting everything a bit closer together.
You have the other surf statement in the loop, so I don't know why you left "surf(x,y,z)" in there.
"valueslabel" is not a built-in function, nor is it defined in the file, so that had to go for me.
I didn't mean to use this code literally:
surf(...,'EdgeColor','none','FaceColor','interp')
What I meant was for any call to "surf", you could add in some Name-Value pair arguments at the end to turn off edges and make the shading more interpolated. For example:
surf(x,y,z(k)*ones(size(values,1),size(values,2)),values(:,:,k),'EdgeColor','none','FaceColor','interp')
Also may want to throw a:
view(3)
at the end to make it automatically view the surfaces from a 3-D perspective. When I do all this, it works for me.
Süleyman Köken
on 25 May 2017
Categories
Find more on Surface and Mesh 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!