Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Creating a surface from txt file of XYZ coordinates

Subject: Creating a surface from txt file of XYZ coordinates

From: Susan Tully

Date: 6 Mar, 2013 15:41:08

Message: 1 of 6

Hello, I am trying to use the surface function to produce a coloured image of my surface. I have a txt file containing x,y,z with comma delimiter as below:

265000.000000,835000.000000,259.400000
265000.000000,835010.000000,259.300000
265000.000000,835020.000000,259.200000
265000.000000,835030.000000,259.100000
265000.000000,835040.000000,259.000000
265000.000000,835050.000000,258.900000
265000.000000,835060.000000,258.800000
265000.000000,835070.000000,258.600000
...

Here is what I have tried:

>> XYZ=importdata('ALL_DTM_txt.txt');
>> surf(XYZ(:,1),XYZ(:,2),XYZ(:,3));
??? Error using ==> surf at 70
      Z must be a matrix, not a scalar or vector.

>> [X,Y,Z]=XYZ;
??? Too many output arguments.

>> [X,Y,Z]=[XYZ(:,1),XYZ(:,2),XYZ(:,3)];
??? Too many output arguments.

>> [X,Y,Z] = importdata('ALL_DTM_txt.txt');
>> surfc(X,Y,Z);
??? Invalid surface property: ','.

      Error in ==> surf at 93
      hh = double(graph3d.surfaceplot(args{:},'parent',parax));

      Error in ==> surfc at 53
      hs=surf(cax,args{:});

>> [X,Y,Z] = importdata('ALL_DTM_txt.txt',',');
>> surfc(X,Y,Z);
??? Invalid surface property: ','.

     Error in ==> surf at 93
     hh = double(graph3d.surfaceplot(args{:},'parent',parax));

     Error in ==> surfc at 53
     hs=surf(cax,args{:});

If anyone could shed some light on what I am doing wrong that would be much appreciated!

Susan.

Subject: Creating a surface from txt file of XYZ coordinates

From: thomas.ibbotson@mathworks.co.uk

Date: 6 Mar, 2013 16:05:20

Message: 2 of 6

Susan Tully <s.tully@ed.ac.uk> wrote:
> Hello, I am trying to use the surface function to produce a coloured image of my surface. I have a txt file containing x,y,z with comma delimiter as below:
>
> 265000.000000,835000.000000,259.400000
> 265000.000000,835010.000000,259.300000
> 265000.000000,835020.000000,259.200000
> 265000.000000,835030.000000,259.100000
> 265000.000000,835040.000000,259.000000
> 265000.000000,835050.000000,258.900000
> 265000.000000,835060.000000,258.800000
> 265000.000000,835070.000000,258.600000
> ...
>

See the "Reshaping Data" section of:
http://www.mathworks.com/help/matlab/visualize/representing-a-matrix-as-a-surface.html

this shows how you can change your data into rectangular grids that can
be accepted by the various plotting functions.

Cheers,
Tom

Subject: Creating a surface from txt file of XYZ coordinates

From: Susan Tully

Date: 7 Mar, 2013 11:50:08

Message: 3 of 6

> See the "Reshaping Data" section of:
> http://www.mathworks.com/help/matlab/visualize/representing-a-matrix-as-a-surface.html
>
> this shows how you can change your data into rectangular grids that can
> be accepted by the various plotting functions.
>
> Cheers,
> Tom

Thanks for the help, I didn't know about the reshape function so that was great! I had been trying with griddata but couldnt get it to work.

I have discovered an issue with my data I think, I thought I had 1001 X values and 1001 Y values and so tried:

>> reshapeX = reshape(X,1001,1001);
??? Error using ==> reshape
To RESHAPE the number of elements must not change.

When I checked using "res = [unique(X(:)), histc(X(:),unique(X(:)))]" It turns out my minimum and maximum X only occurs 1000 times, all increments inbetween occur 1001 times. Same with my Y values. I tried again after adding the two lines I was missing. Now I can use the reshape function without error but when I try to use the surf function I get:

reshapeX = reshape(X,1001,1001);
reshapeY = reshape(Y,1001,1001);
reshapeZ = reshape(Z,1001,1001);
surf(X,Y,Z)
??? Error using ==> surf at 70
Z must be a matrix, not a scalar or vector.

Any ideas as to why it is still saying that?

thanks in advance,

Susan

Subject: Creating a surface from txt file of XYZ coordinates

From: thomas.ibbotson@mathworks.co.uk

Date: 7 Mar, 2013 11:54:45

Message: 4 of 6

Susan Tully <s.tully@ed.ac.uk> wrote:
> reshapeX = reshape(X,1001,1001);
> reshapeY = reshape(Y,1001,1001);
> reshapeZ = reshape(Z,1001,1001);
> surf(X,Y,Z)
> ??? Error using ==> surf at 70
> Z must be a matrix, not a scalar or vector.
>
> Any ideas as to why it is still saying that?

I think you want:

surf(reshapeX, reshapeY, reshapeZ);

not surf(X, Y, Z);

Cheers,
Tom

Subject: Creating a surface from txt file of XYZ coordinates

From: Susan Tully

Date: 7 Mar, 2013 12:05:08

Message: 5 of 6

 
> I think you want:
>
> surf(reshapeX, reshapeY, reshapeZ);
>
> not surf(X, Y, Z);
>
> Cheers,
> Tom

Doh! That works. Thanks for your help and patience

Susan

Subject: Creating a surface from txt file of XYZ coordinates

From: Steven_Lord

Date: 7 Mar, 2013 15:11:59

Message: 6 of 6



"Susan Tully" <s.tully@ed.ac.uk> wrote in message
news:kh9utg$se4$1@newscl01ah.mathworks.com...
>> See the "Reshaping Data" section of:
>> http://www.mathworks.com/help/matlab/visualize/representing-a-matrix-as-a-surface.html
>>
>> this shows how you can change your data into rectangular grids that can
>> be accepted by the various plotting functions.
>>
>> Cheers,
>> Tom
>
> Thanks for the help, I didn't know about the reshape function so that was
> great! I had been trying with griddata but couldnt get it to work.
>
> I have discovered an issue with my data I think, I thought I had 1001 X
> values and 1001 Y values and so tried:
>
>>> reshapeX = reshape(X,1001,1001);
> ??? Error using ==> reshape
> To RESHAPE the number of elements must not change.

Your line of code will work if X contains 1001*1001 = 1002001 elements.
Because you received an error, that must not be the case.

> When I checked using "res = [unique(X(:)), histc(X(:),unique(X(:)))]" It
> turns out my minimum and maximum X only occurs 1000 times, all increments
> inbetween occur 1001 times.

So X had 1001999 elements when you tried to RESHAPE it above?

> Same with my Y values. I tried again after adding the two lines I was
> missing. Now I can use the reshape function without error but when I try
> to use the surf function I get:
>
> reshapeX = reshape(X,1001,1001);
> reshapeY = reshape(Y,1001,1001);
> reshapeZ = reshape(Z,1001,1001);
> surf(X,Y,Z)
> ??? Error using ==> surf at 70
> Z must be a matrix, not a scalar or vector.
>
> Any ideas as to why it is still saying that?

Thomas answered that one.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us