MATLAB Answers

0

How to prepare and plot unequally spaced, irregular data to a contour plot or similar

Asked by usr0815 on 2 May 2017
Latest activity Edited by usr0815 on 3 May 2017
Hey there,
I've been looking for ages now, but I just don't find a solution that solves my problem. I am pretty sure that it can be solved with some combination of reshaping, ndgrid/mesh, or gridded interpolation but I just don't get it.
I've a data set of hot-wire measurement velocities that have been sequentially recorded on specific locations in the xz-plane with a traverse-system. The problem is that the data is not a full uniform grid, but sparse. Actually, the traversing process was performed in a way to get several locations within a given area, while avoiding hitting a circular obstacle. Additionally the area close to the obstacle was sampled more densely as compared to regions farther away from the obstacle.
As a result I have a matrix with 3 columns and for example 400 rows in the following way:
x z values (3 columns)
x1 z1 value1
x2 z2 value2
x3 z3 value3
. . .
. . .
. . .
But the problem is:
  1. the data was collected in some wierd order, e.g. the first 6 coordinates (x-component) are [-5; -5; -3; 10; 10; 0.8; ...] and the first 6 coordinates (z-component) are [0.2; 0.2; 5; 3; 2; -1; ...] ...
  2. the data does not represent a uniform and equally spaced grid: all data (for example) lies within the range [-5 <= x <= 10] and [-2 <= z <= 6], but it is possible that there are 8 z-positions for one x-position, while at another x-position there are only 3 z-positions and at several other x-positions there are no z-positions at all (as within the obstacle region).
  3. But there is a greatest common grid resolution divisor, e.g. 0.1 so all x and z-coordinates are positive or negative multiples of 0.1. This means it would be possible to generate a huge matrix (xz-plane) that includes all possible locations. I think this could help somehow.
What I need basically: I just want a simple contourf plot of the data within the xz-plane and areas that have not been recorded are either interpolated or better filled with NaNs or Zeros or something like that. Actually, I know the coordinates of the obstacle and might just draw it as overlay later.
Do you know how to get my desired result in a convenient manner?

  1 Comment

I found a possible solution with the help of some collegue.
When 'grid' is the matrix containing an 'x' and a 'z' and a 'value' column (each having the same number of elements):
[X,Y] = ndgrid(linspace(min(grid(:,1)),max(grid(:,1)),150),linspace(min(grid(:,2)),max(grid(:,2)),150));
Z = griddata(grid(:,1),grid(:,2),grid(:,3),X,Y,'cubic');
contourf(X,Y,Z)
axis equal
scatter(grid(:,1),grid(:,2),10,grid(:,3),'wx');
So basically
  1. a grid is generated with a relativly fine resolution (150x150 points)
  2. the original data is interpolated onto this grid with a cubic interpolation method
  3. contourf plot is generated and axis is set equal
  4. the original data points are scattered onto the contourf plot to make sure that the interpretation of the data is somehow more reliable and one can see at once that the areas are interpolated.
I realized in between that the real difficulty is the obstacle area, as is gets wierdly interpolated although there should be no data, as none of the interpolation methods is used to such boundaries or optimized to those.

Sign in to comment.

0 Answers