+ Reply to Thread
Results 1 to 2 of 2

Help with adapting a matlab code to vba

  1. #1
    Registered User
    Join Date
    12-26-2008
    Location
    Sweden
    Posts
    10

    Help with adapting a matlab code to vba

    Hi,
    I want to calculate the distance between two points which i have in longitude and latitude. I have been provided with a matlab code which solves my problem, but i dont have matlab. Can someone help me convert this so i can use excel to solve it?
    Thanks in advance.

    The code:
    %EARTHDIST(Xlong,Ylat,choice) An m-file to compute the distance between the
    % points given in the Xlong and Ylat arrays. Returns an array of N-1
    % distances where N is the number of points given (must be >= 2).
    % Latitudes and longitudes must be in decimal degrees.
    %
    % There are two ways to do this:
    % choice = 1 : distances are between sequential points
    % choice = 2 : distances are between each point and the last.

    function d=earthdist(x,y,choice)
    N=length(x);
    if N<2
    disp('Must have at least two points')
    else
    if choice==1
    Re=6356.912;
    rad=2*pi/360;
    alpha=rad.*y(1:N-1)-rad.*y(2:N);
    beta=rad.*x(1:N-1)-rad.*x(2:N);
    phi=(rad.*y(1:N-1)+rad.*y(2:N))./2;
    d=Re.*(alpha.^2+beta.^2 .*cos(phi).^2).^0.5;
    else
    Re=6356.912;
    Last edited by h3lla; 03-07-2012 at 03:16 AM.

  2. #2
    Forum Expert sweep's Avatar
    Join Date
    04-03-2007
    Location
    Great Sankey, Warrington, UK
    MS-Off Ver
    2003 / 2007 / 2010 / 2016 / 365
    Posts
    3,454

    Re: Help with adapting a matlab code to vba

    Mr Pearson has it all on his website:

    http://www.cpearson.com/excel/LatLong.aspx
    Rule 1: Never merge cells
    Rule 2: See rule 1

    "Tomorrow I'm going to be famous. All I need is a tennis racket and a hat".

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1