How to I convert this to a 2d array?

4 visualizaciones (últimos 30 días)
Ewen Chan
Ewen Chan el 2 de Abr. de 2016
Comentada: Star Strider el 4 de Abr. de 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?
Your help is greatly appreciated.

Respuesta aceptada

Star Strider
Star Strider el 2 de Abr. de 2016
Editada: per isakson el 2 de Abr. de 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 comentarios
Ewen Chan
Ewen Chan el 4 de Abr. de 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
Star Strider el 4 de Abr. de 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.

Iniciar sesión para comentar.

Más respuestas (3)

Muhammad Usman Saleem
Muhammad Usman Saleem el 2 de Abr. de 2016
Solution is here
Please check it and accept my answer if this fulling your requirements
  1 comentario
Ewen Chan
Ewen Chan el 2 de Abr. de 2016
Editada: Ewen Chan el 2 de Abr. de 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.

Iniciar sesión para comentar.


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

Iniciar sesión para comentar.


Ewen Chan
Ewen Chan el 2 de Abr. de 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.

Categorías

Más información sobre Surface and Mesh Plots en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by