Read LiDar/LAS file in matlab.

Below is the c code for reading LiDar/LAS file into matlab. To compile the code, simply use
mex LASReader.cpp

The code reads the points coordinates only. Please see the code for details.

For details of LiDar file format, please visit this link.

#include "mex.h"
//#include <math.h>
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* This is a simple code to read LiDar file in matlab. It only reads only point coordinates now.
 * Syntax : points = LASReader('filename.las');
 *     points is a matrix of size 3 x numOfPoints.
 * Please email me at if you have any questions.
 * Jason Yu-Tseh Chi

using namespace std;

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
    if(nrhs != 1)
        mexErrMsgTxt("Error: Syntax: reader(filename);\n");

    //mexPrintf("begin reader\n");
    int sLength;
    // size of file header
    int headerSize = 227;
    mxArray *lData;
    char *fname;
    unsigned char header[227];
    unsigned int nPoints;
    //double xScale, yScale, zScale, xOffset, yOffset, zOffset;
    double scales[3],offsets[3], mins[3], maxs[3];

    int pRecLength, offset2Data;

    // read filename
    // get the length
    sLength = mxGetN(prhs[0])+1;
    fname = new char[sLength];
    // copy the filename
    mxGetString(prhs[0], fname, sLength);
    mexPrintf("The file to be opened is %s\n\n", fname);

    // open the LiDar file
    ifstream ifs(fname, ios::in|ios::binary);
    {   mexErrMsgTxt("Error! File open error!");

    // go to the end of the file to get the fileSize
    ifs.seekg(0, ios::end);
    int fileSize = ifs.tellg();
    ifs.seekg(0, ios::beg);

    // read the file header first;*)header, headerSize);

    // read file signature. It has to be LASF otherwise it's not a valid LAS file.
    char four[5];
    four[4] = '\0';
    mexPrintf("======= File INFO ======\n");
    mexPrintf("File Signature:\t\t %s (If it's not LASF, this file is not a valid LIDar file.)\n",four);

    // header size
    unsigned short temp;
    memcpy((unsigned char*)&temp,header+94,2);
    mexPrintf("Header Size: \t\t %d\n", (int)temp);

    // Offset to Data. Will use this value later when reading points data.
    unsigned long temp1;
    memcpy((unsigned char*)&temp1, header+94+2, 4);
    offset2Data = (int)temp1;
    mexPrintf("Offset to Data: \t %d\n", (int)offset2Data);

    // FOrmat ID.
    unsigned char t1;
    mexPrintf("Point Data Format ID:\t %d\n", (int)t1);

    // Point record length. Will use this value later when reading points data.
    memcpy((unsigned char*)&temp,header+94+2+4+4+1,2);
    pRecLength = (int)temp;
    mexPrintf("Point Record Length: \t %d\n", pRecLength);

    // Number of points. Will use this value later when reading points data.
    memcpy((unsigned char*)&temp1,header+94+2+4+4+2+1,4);
    nPoints = temp1;
    mexPrintf("Number of Points: \t %d\n", (int)nPoints);

    // Scales and offsets.
    double temp2;
    memcpy((unsigned char*)&temp2,header+131,8);
    scales[0] = temp2;
    memcpy((unsigned char*)&temp2,header+131+8,8);
    scales[1] = temp2;
    memcpy((unsigned char*)&temp2,header+131+16,8);
    scales[2] = temp2;
    mexPrintf("Scales: \t\t (%.5f, %.5f, %.5f)\n", scales[0], scales[1], scales[2]);

    memcpy((unsigned char*)&temp2,header+155,8);
    offsets[0] = temp2;
    memcpy((unsigned char*)&temp2,header+155+8,8);
    offsets[1] = temp2;
    memcpy((unsigned char*)&temp2,header+155+16,8);
    offsets[2] = temp2;
    mexPrintf("Offsets: \t\t (%.5f, %.5f, %.5f)\n", offsets[0], offsets[1], offsets[2]);

    // max's and min's in X, Y, and Z direction
    memcpy((unsigned char*)&temp2,header+179,8);
    maxs[0] = temp2;
    memcpy((unsigned char*)&temp2,header+179+8,8);
    mins[0] = temp2;
    memcpy((unsigned char*)&temp2,header+179+16,8);
    maxs[1] = temp2;
    memcpy((unsigned char*)&temp2,header+179+24,8);
    mins[1] = temp2;
    memcpy((unsigned char*)&temp2,header+179+32,8);
    maxs[2] = temp2;
    memcpy((unsigned char*)&temp2,header+179+30,8);
    mins[2] = temp2;

    mexPrintf("MaxXYZ: \t\t (%.5f, %.5f, %.5f)\n", maxs[0], maxs[1], maxs[2]);
    mexPrintf("MinXYZ: \t\t (%.5f, %.5f, %.5f)\n", mins[0], mins[1], mins[2]);


    // temp for reading one point
    long coor[3];
    // pointer to
    double *x;

    // will return a size of 3 x NumberOfPoints matrix back to matlab.
    plhs[0] = mxCreateDoubleMatrix(3, nPoints,mxREAL);
    x =mxGetPr(plhs[0]);

    // move to the data section of the file.
    // read point by point
    for(int i=0;i<nPoints;i++)

        // consider the offsets and scales.
        x[i*3] = (double)coor[0]*scales[0] + offsets[0];
        x[i*3+1] = (double)coor[1]*scales[1] + offsets[1];
        x[i*3+2] = (double)coor[2]*scales[2] + offsets[2];

    delete [] fname;

24 Responses to Read LiDar/LAS file in matlab.

  1. You actually make it appear so easy with your presentation however I find this
    topic to be really one thing that I think I would never understand.
    It seems too complex and very huge for me. I’m looking forward for your subsequent submit, I will try to get the hang of it!

  2. Turn the dough once to coat, then cover the bowl with a
    damp dishtowel. Remember to pre-heat your oven for at least 1 hour.

    It is not like ordinary dough, it is not supposed to be soft as a baby’s bottom.

  3. After I originally commented I appear to have clicked on the -Notify me when new comments are added- checkbox and now whenever a comment is added I get four emails with the
    same comment. There has to be a way you can remove me from that service?
    Thank you!

  4. augenlasern says:

    It’s perfect time to make some plans for the future and it’s time to be
    happy. I have learn this submit and if I may I wish to recommend you few interesting things or advice.
    Maybe you can write next articles referring to this article.
    I wish to learn even more things approximately it!

  5. Carey says:

    Thanks a lot for composing “Read LiDar/LAS file in matlab.
    | Jason Yu-Tseh Chi’s Notes”. Imight really be returning for more reading and commenting soon
    enough. Thank you, Lisa

  6. forenames says:

    What’s up everyone, it’s my first visit at this web site, and paragraph is genuinely fruitful in favor
    of me, keep up posting these types of posts.

  7. You can use tethered or untethered jailbreak methods. It began off with books, moved onto the
    E-book reader, thus revolutionizing the studying globe. And I was
    so excited even although I had place it off, and put it off.

  8. iReb r6 says:

    Dealing with the very best of the blacklist is difficult.

    Failure to do so will result in the device resetting
    to an us-jailbroken state. There is a large amount of dread as well as a
    lot of frustration.

  9. says:

    The overall game play is quite simple-but it excels at its hiphop imagination. Earlier this week
    the Federal Trade Commission given authorization for Electronic Arts (NASDAQ: ERTS) to get Take-Two.

  10. Users of WhatsApp and other chat customers are frequently stated
    to be “texting” without a texting strategy. Have you ever used Google’s voice
    search or Apple’s Siri? You can’t install whatsapp and chat to somebody on Nimbuzz.

  11. The last application in this list, which requires the
    tenth position is a game. Your extremely basic software known as BIOS.
    To browsing and play 720p video clip, Z10 can display his very best overall performance.

  12. Octavia says:

    Acum sa nu – obține pe termen scurt și spune
    că semnul în sine este tot ce e de dragostea ta .
    Lumini de neon și muzică fantastic sunt , în general, o remiză pentru bărbați și femei care iubesc clubbing .
    Cred că am mâncat stafide acoperite cu iaurt prea abundente

  13. Can you run Skype on a non Wifi community for totally free?
    You currently got an iPhone, now you received the new iPhone 4 or iPod touch.
    Often, you will be offered a paper sport sheet to use with your Mexican Jumping Beans.

  14. They’ve enjoyed in 25 of the past 27 playoffs. Let’s confess it, a Chiefs solution doesn’t come
    cheap. I really don’t want a back up strategy,
    I need that certain to work out.

  15. It is just a matter of hundred dollars, though. 1) Jailbreaking change themes, settings and also customize your device better-than before.
    Looked just like the Apple men knew they certainly were planning to be rich.

  16. Many speakers are happy if they’re currently coming to the area to match.
    They often times have important input into AV selections, design, and marketing.
    You will find a lot of television supports to choose from.

  17. We were offered an enhanced iPhone 4 instead. This will permit you
    to choose the restore stage we just made before starting this whole process.
    Not all merchants on-line will accept a return, and you should shy absent
    from them.

  18. You now have the power to use your Apple iphone as a house video telephone with unlimited minutes!

    This gives you ultimate protection and less worries.
    Let us find out about the most popular messaging app.

  19. Site Stopping – stop access to pre-explained
    internet websites. At that time it experienced been one of many newest programs and it did the trick adequately for
    me individually.

  20. Hope you have enjoyable with your new jailbroken iPhone.
    For individuals on the go, skinny wallets are all
    the rage. At any time think of obtain any applications for iPad 3 even if they are not integrated
    the Application Store?

  21. But before you start, call your bank, up your credit
    card limit, its time to begin spending! Additionally there are several who
    want to look fantastic with the opposite gender. Physician’s Orders is #9 in the bingo

  22. ho says:

    So glad I found this great information, thanks

  23. I have read so many content regarding the blogger
    lovers but this article is truly a nice post, keep it up.

  24. Quality articles is the important to attract the people to pay a visit the site, that’s what
    this web page is providing.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: