MATLAB Answers

How to I convert this to a 2d array?

26 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});
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.

Accepted Answer

Star Strider
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.
  10 Comments
Star Strider
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.

Sign in to comment.

More Answers (3)

Muhammad Usman  Saleem
Muhammad Usman Saleem on 2 Apr 2016
Solution is here
Please check it and accept my answer if this fulling your requirements
  1 Comment
Ewen Chan
Ewen Chan on 2 Apr 2016
Yeah, I tried that too. Couldn't make heads or tails of it because I think that my question is a LOT simpler than what you had.
And it also looks like that the code that's on that page only writes it out as separate x y z text files.
What I'm looking for is to actually generate the 2d array so that I can plot it using contourf.
Thanks.

Sign in to comment.


Kuifeng
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);
  1 Comment
Ewen Chan
Ewen Chan on 2 Apr 2016
Unfortunately, I specifically need a contour plot (because it's going to a paper publication). Thank you for your help though.

Sign in to comment.


Ewen Chan
Ewen Chan on 2 Apr 2016
This is what the 3D surface looks like in CAD.
But I am looking for a 2D contour plot because it is going into a print publication.
Maybe this would help?
So I'm looking/hoping to be able to plot the data as-is.
Thank you.

Community Treasure Hunt

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

Start Hunting!

Translated by