I have a big file with 3 columns: density, dimension, value.

10 0.3 200  
10 0.4 300  
20 0.3 250  
20 0.4 320  

I am trying to draw a 3d plot - mesh with mesh() function in octave, like this:

data = load ("file.txt");  
mesh(data(:,1), data (:,2), data (:,3));

Problem I have is , I always get error:

rows (z) must be the same as length (y), columns (z) must be the same as length (x).

It worked with function plot3(), but I would like a mesh kind of plot.

  • $\begingroup$ Welcome to SciComp.SE! While Octave and MATLAB-related questions are on topic here, they are expected to have a scientific focus. This question can be answered by simply reading the MATLAB documentation for the mesh function. Note that if X is of size $n\times 1$ and Y is of size $m\times 1$, then Z is expected to be of size $n\times m$ in mesh(X,Y,Z). $\endgroup$
    – Ben
    Commented Jun 3, 2013 at 3:41
  • $\begingroup$ Thanks. I am sorry for that, but purpose is purely scientific-al. I knew about the dimensions, just didn't know how to apply my data to that function. reshape() function is the answer! $\endgroup$
    – kle pra
    Commented Jun 3, 2013 at 16:57

2 Answers 2


The mesh function plots functions z=f(x,y). So to call the mesh() function, you must have 2D data. You can give vectors for x and y, but z must be an array with length(x) rows and length(y) columns, or x and y and z must be all be 2D arrays of the same size. Your data has been pulled out into a single, long vector which you need to two-dimensionalize.

I believe the reshape function will do what you need. E.g. something like:


where m and n are the appropriate dimensions in x and y of your data.

  • $\begingroup$ Thanks for explaining and reshape function! I manually entered x,y vectors since I know the interval: start, end and step and reshaped z-data and it works now. $\endgroup$
    – kle pra
    Commented Jun 3, 2013 at 16:53
  • $\begingroup$ There's also a vec2mat function in MATLAB, but I didn't find its counterpart in Octave. reshape goes back a long way, but vec2mat may be new. $\endgroup$
    – Bill Barth
    Commented Jun 4, 2013 at 16:42

Another way could be:

Call your 3 data vectors aa,bb,cc and assume that aa and bb represent a point on the plane and cc is the height above that plane.

Create a grid:

x_plot = linspace(min(aa),max(aa),100);
y_plot = linspace(min(bb),max(bb),100);
[XI,YI] = meshgrid(x_plot,y_plot);

Create the interpolant:

ZI = griddata(aa,bb,cc,XI,YI);

And plot


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.