33 views (last 30 days)

I have a space delimited text file in the form of x y z and I want to create a 2d array so that I can plot the contour (using contourf) - what's the best way I can do that?

This is what the data looks like: (x y z)

23.48497963 13.19587040 60.91899872

24.03828049 13.26920033 61.01649857

24.63999939 13.42000008 60.59999847

24.76104927 13.37959957 61.06637955

18.97845078 12.31340027 61.88624954

19.23686028 12.50454998 62.81980896

19.55764008 12.43560028 61.80752945

And for the contourf function, it says that I need to format that into a 2d array (and I need to have the x and y be the indices.

I tried this:

f=fopen('68 data set.txt');

c=textscan(f,'%f %f %f','CollectOutput',true);

fclose(f);

out=accumarray(c{1}+1,c{2});

(from this: http://www.mathworks.com/matlabcentral/answers/116044-how-to-read-data-from-txt-file-and-to-create-a-2d-matrix )

And it gave an error: "Index exceeds matrix dimensions"

Also, is it true that I would have to sort the indices for the contourf function?

Your help is greatly appreciated.

Star Strider
on 2 Apr 2016

Edited: per isakson
on 2 Apr 2016

You can do everything with the data you have and the scatteredInterpolant function. It is a core MATLAB function, and no Toolboxes are required. I was able to do this much with the data you presented:

XYZ = [ 23.48497963 13.19587040 60.91899872

24.03828049 13.26920033 61.01649857

24.63999939 13.42000008 60.59999847

24.76104927 13.37959957 61.06637955

18.97845078 12.31340027 61.88624954

19.23686028 12.50454998 62.81980896

19.55764008 12.43560028 61.80752945];

F = scatteredInterpolant(XYZ(:,1:2), XYZ(:,3), 'linear'); % ScatteredInterpolant, Linear Interpolation

[Gx,Gy] = meshgrid(XYZ(:,1), XYZ(:,2)); % Define ‘meshgrid’ As Query Points

%

figure(1)

contourf(F(Gx,Gy)) % Create Filled Contour Plot

grid

With all our data it would likely look much better. It does create a contourf plot from your data.

I am not certain where you want to go with it from here, but this code works. I didn’t post the plot because of the small amount of data you presented. With all your data it should give you the information you want. You can generate the plot I created from this code.

Star Strider
on 4 Apr 2016

If it does not model your surface accurately, there are a number of interpolation methods that might be better, such as 'nearest', 'linear', natural, and 'v4'. I would experiment with each to see which produces the best result. I chose 'cubic' because that is usually the best for complicated curved surfaces, but it may not be ideal in your application.

With respect to the functions connecting points on the upper (with respect to ‘Y’) concave part of your surface, the problem may be in the way your data are organised. The plot functions want to ‘connect the lines’ to produce a convex outline. Even delaunay and boundary (which should produce an accurate boundary even along concave regions of your surface) colour outside your intended border. Every function I’ve experimented with wants to connect the points along the concave curvature. Even the surf and mesh plots connect that region. Your data have defeated everything I can think of to not connect that region.

You can get some idea of what the problem is by plotting:

figure(3)

plot3(X, Y, Z, '-x', 'MarkerSize',5)

grid on

% view([0 90])

Enlarging it in the plot GUI will give you some idea. The plotted lines zig-zag.

Since I have no idea how your data were collected or how they are organised, I’m not certain what to suggest. Sorting them could be an option, but I have no idea how best to do that, since I don’t know anything about your data. (You would use sortrows to sort ‘D{:}’ by one of its columns.)

Star Strider
on 4 Apr 2016

My pleasure.

It helps a bit. When I sort your data by any one of the three columns, it doesn’t change the way MATLAB displays it. Using boundary helps somewhat, but several of the points are still connected where they shouldn’t be.

There are many undocumented features in MATLAB code, that only MathWorks knows of. At this point, I suggest that you Contact Support, submit your problem as a bug report or enhancement request, and see if MathWorks can fix it. This isn’t really an ‘extrapolation’ but the problem of MATLAB creating continuities where there are none.

Include the URL of this thread: http://www.mathworks.com/matlabcentral/answers/276683-how-to-i-convert-this-to-a-2d-array in your message to MathWorks so you don’t have to repeat yourself. Just mention that this thread fully describes your problems and my attempts to solve them.

Ask MathWorks to email me with any solution they come up with, because I’d like to know if one exists. I am out of ideas.

Muhammad Usman Saleem
on 2 Apr 2016

Solution is here

Please check it and accept my answer if this fulling your requirements

Kuifeng
on 2 Apr 2016

Maybe can try the function plot3 instead of contourf.

f=importdata('68 data set.txt'); x = f(:,1); y = f(:,2); z = f(:,3); plot3(x,y,z);

Opportunities for recent engineering grads.

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

Start Hunting!
## 0 Comments

Sign in to comment.