How to display coordinates of points in "contourf"?

I have three matrices, X, Y, and Z. I am creating a filled contour plot using the command
contourf(X,Y,Z)
How can I display the coordinates of all points in the plot? I can use "Data Cursor" in figure environment to mark coordinates of points individually but this may take a lot of time depending on the number of points.

4 Comments

What do you mean by displaying coordinates? The coordinates are given by the y- and x-axes and the z-value is given by the colorbar.
Dear Jonas,
Thank you for your comment.
What I want to do is have a text box with all points showing their coordinates. Is there any built-in function for this?
Still unclear. Do you want to overlay the contourf with a grid showing all coordinates, like [x1,y1]? All coordinates included?
Yes! this is exactly what I want to do. Thank you!

Sign in to comment.

 Accepted Answer

jonas
jonas on 6 Aug 2018
Edited: jonas on 6 Aug 2018
It's going to look real messy when you have high resolution x and y data.
%%Some data
[x,y,z]=peaks(20);
[~,c]=contourf(x,y,z);hold on
set(gca,'XTick',[],'YTick',[]);
%%Create cell array with coordinates
C = reshape([x(:)'; y(:)'], [], 1)';
str=sprintf('[%.2g;%.2g],',C);
str=strsplit(str,',')
str(end) = [];
%%Plot labels
text(x(:),y(:),str(:),'fontsize',6,'horizontalalignment','center','verticalalignment','mid')
Or alternatively, with the undocumented sprintfc (courtesy Walter Roberson)
%%Same code as above, until:
str=sprintfc('[%.2g;%.2g]',C);
text(x(:),y(:),str(:),'fontsize',6,'horizontalalignment','center','verticalalignment','mid')

4 Comments

A portion of that could be optimized in that text() accepts multiple coordinates and cell array of character vectors. But then you need to do a vectorized sprintf to generate the character vectors.
jonas
jonas on 6 Aug 2018
Edited: jonas on 6 Aug 2018
Oh, I tried multiple inputs at first. Guess I used the wrong syntax. Sometimes I should take my own advice and read the doc :)
Will update the solution
sprintfc() might come in handy. Unfortunately it is undocumented.
jonas
jonas on 6 Aug 2018
Edited: jonas on 6 Aug 2018
Managed to do it with sprintf, although it was a bit of a hassle. Will definitely look into sprinfc! Thanks

Sign in to comment.

Categories

Products

Asked:

on 6 Aug 2018

Answered:

on 8 Oct 2019

Community Treasure Hunt

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

Start Hunting!