# How to I convert this to a 2d array?

3 visualizzazioni (ultimi 30 giorni)
Ewen Chan il 2 Apr 2016
Commentato: Star Strider il 4 Apr 2016
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?
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Risposta accettata

Star Strider il 2 Apr 2016
Modificato: per isakson il 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 CommentiMostra 8 commenti meno recentiNascondi 8 commenti meno recenti
Ewen Chan il 4 Apr 2016
Generically, the data is collected by running a series of experiments, varying two parameters (one that is shown as the x-axis, and another that isn't shown) which is how I created the surface in CAD (think along the lines of a multi-section loft, except that in reality, it isn't quite that - but something similiar to that idea - if that helps you understand where the data is coming from).
And then the surface is meshed/tessellated inside the CAD environment and the resulting text file is the export of the mesh.
Hope that helps.
So what I am hoping to get out of this is to be able to plot the data "as-is" without any interpolation or extrapolation.
Thanks.
Star Strider il 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.

Accedi per commentare.

### Più risposte (3)

Muhammad Usman Saleem il 2 Apr 2016
Solution is here
##### 1 CommentoMostra -1 commenti meno recentiNascondi -1 commenti meno recenti
Ewen Chan il 2 Apr 2016
Modificato: Ewen Chan il 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.

Accedi per commentare.

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

Accedi per commentare.

Ewen Chan il 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.
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Categorie

Scopri di più su Contour Plots in Help Center e File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by