X,Y data of irregular grid, scatter plot with Z data

2 views (last 30 days)
Dear all,
I know that there is the possibility to create a scatter plot using X and Y and reshaping the Z data, such that:
Z = reshape(Z, [X Y]);
figure()
imagesc (Z)
Square_xyz_2D_scatterplot.png
This creates a square 2D plot like depicted above.
However, I have a different problem now in which X,Y,Z data is already given to me (see text file attached).
The X,Y data include the coordinates of the center of a square. Z is the data that I would like to visualize by a scatter plot just like the example above. Now my 2D 'grid' on which I like to depect my Z data on looks like this:
irregular_2D_XY.jpg
So I have 2 questions
  1. Is there an easy way to create a nice rectangular grid out of these center points (still working with Matlab 2016, so no rectangle function yet...)? Or from the coordinates that I have on each node of one rectangular (see txt file attached).
  2. How to include my Z data such that I can create a scatter plot?
-> I know it is not a highly complicated problem, but I'm afraid that I might miss out some easy Matlab functions that are very convenient to use in my case.
Many thanks for any tips

Accepted Answer

Star Strider
Star Strider on 22 Jan 2020
It is not obvious to me what you want.
You already appear to have tried reshape, although I have no idea what the arguments to it were, since we do not have that part of your code.
Try this:
filename = 'Demonstration_data_xyz.txt';
fidi = fopen(filename,'rt');
Dc = textscan(fidi, '%f%f%f%f%f%f%f%f%f%f%f%f', 'HeaderLines',1', 'CollectOutput',1, 'EndOfLine','\r\n', 'Delimiter',' ', 'MultipleDelimsAsOne',1);
Dd = [Dc{:}];
XYZ = Dd(:,10:12);
didx = diff([1; find(diff([0; XYZ(:,1)]) < 0));
Xmtx = reshape(XYZ(:,1), mean(didx), []);
Ymtx = reshape(XYZ(:,2), mean(didx), []);
Zmtx = reshape(XYZ(:,3), mean(didx), []);
figure
surf(Xmtx, Ymtx, Zmtx)
If this does not do what you want, please describe it in more detail. (I kept to functions that I remember being part of R2016b.)
  3 Comments
Willemijn Wolf
Willemijn Wolf on 23 Jan 2020
Thank you for your help and sorry for the misunderstanding.
I was thinking in too difficult solutions while just using surf is exactly doing what I wanted

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!