Extracting contourf data from superimposed line

Hello,
I have a contourf plot with a line superimposed on the plot. I'd like to extract the values of the contour for each coordinate point of the superimposed line, much like the Data Cursor tool in the figure window. The only way I can think to do this is to interpolate between points of the line with the spatial contour coordinates, but this isn't a very accurate solution since my spatial grid isn't extremely fine (to create the grid I have to perform some numerical integration and it takes a long time, also I'm not entirely sure how to do this solution). Can anyone think of a better way to do this?
Thanks for any help, Patrick

8 Kommentare

KSSV
KSSV am 30 Sep. 2016
Can you attach an image..so that we can get some idea..
I've attached an image of the contour plot. The green slanted line towards the bottom are the locations which I'd like to extract the contour data.
You can get the contour information as (x,y) pairs for each contour if you request outputs from the contour function.
Read the documentation for contour before you do this!
The format of the output is easy enough to understand, but finding the values you want within the matrix it returns can be an interesting challenge. This usually requires some experimentation.
Patrick Comiskey
Patrick Comiskey am 30 Sep. 2016
Bearbeitet: Patrick Comiskey am 30 Sep. 2016
So I wrote a code to get the components of each contour, and split the contours up into a cell. The cell contains the contour value, the size of the contour, and then the X and Y coordinates. See the code below (it's probably not the fastest or best way, but it works):
figure
[c1]=contourf(XLmat,YLmat,Pmat,100); %Contour plot
close
%Testing the contourf result to find an integer. An integer is only
%possible if the result is describing the length of the contour
[row,col]=find(rem(c1(2,:),1)==0);
%Creating a matrix where the first row is the value of the contour and the
%second row is the length of the contour
asdf=c1(:,col);
%Now creating the start and end index so that I can separate each contour
%into it's own cell based on it's contour value and size
idx_start(1)=1;
idx_start(2)=asdf(2,1)+2;
for l=3:1:size(asdf,2)
idx_start(l)=idx_start(l-1)+asdf(2,l-1)+1;
end
idx_end=idx_start-1;
idx_end(1)=[];
%The idx_start and idx_end arrays are structured such that the start index
%is where the contour value and length are, and the end index are the
%indices of where the contour ends. Below creates a cell of each individual
%contour. One cell represents one contour, I need special treatment for the
%last contour because the final index is the size of the contourf handle.
for w=1:1:size(asdf,2)-1
contour_number{w}=c1(:,(idx_start(w):idx_end(w)));
end
contour_number{w+1}=c1(:,(idx_start(w+1):size(c1,2)));
Now I need to take the X and Y coordinate values of each contour which I split into cells and compare that with each of my points from the green line. I'm not sure of the best way to do this yet but I'll think about it and update with the result. Let me know if you can think of a better solution or improvement on what I did.
You’re doing essentially what I would do. (Individual programming styles differ.) You probably need to find the appropriate ‘ends’ of the contours to plot your green line (or the data needed to do it). This will be either the maximum or the minimum of the x-values with associated y-values less than 0.2. The ismember, or preferably ismembertol, functions might be useful for that.
Thanks for the ideas Star Strider. I ended up figuring out what I needed to do and I actually went about it a very different way from what I initially tried. I'll explain it here so that this post might help anyone in the future who runs into the same issue.
So with the code above, I found the coordinates of each contour as well as it's isoline value. If I plot some of these contours, I notice that the points which create the contour aren't actually nearly as fine as I thought. See my plot below, the green squares are the line which I'd like the contour values at, the three colored lines are three contours found from my code above (the solid red line is a boundary), and the red plus symbols are the points which created the contour.
I can quite easily refine the mesh of the red plus signs, whereas the points used to make the contour aren't fine at all. As a result of seeing this, I found a built in function in MATLAB called "scatteredInterpolant" which interpolates a set of points on a grid which is exactly what I was looking for in the first place. Note that I couldn't use the other built-in interpolation functions because they required a square grid. My final code is quite simple:
F=scatteredInterpolant(XLmat(:),YLmat(:),Pmat(:));
Vq=F(XuprightBound,YuprightBound);
where XLmat and YLmat are the vectors of points which create the red plus signs at values Pmat, and XuprightBound and YuprightBound are the coordinates of my green squares. The result is Vq, which are the Pmat values interpolated at the green square values.
My pleasure.
I’m glad you got it sorted.
@image analyst
i apply " contourf () " Matlab command on image 111.jpg and i get the result as shown in figure 222.jpg
how can i select only yellow area in the 222.jpg OR select any specific contour from the result of "contourf() " command?

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Kategorien

Mehr zu Contour Plots finden Sie in Hilfe-Center und File Exchange

Produkte

Gefragt:

am 30 Sep. 2016

Kommentiert:

am 6 Feb. 2018

Community Treasure Hunt

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

Start Hunting!

Translated by