# Julia version of Matlab function interp1

**URL:** <https://discourse.julialang.org/t/julia-version-of-matlab-function-interp1/8540>\
**Category:** General Usage\
**Tags:** question\
**Created:** [January 23, 2018, 2:12am UTC](https://discourse.julialang.org/t/julia-version-of-matlab-function-interp1/8540 "2018-01-23T02:12:27Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![andrewning](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andrewning/32/3661_2.png) [@andrewning](https://discourse.julialang.org/u/andrewning)\
**Post date:** [January 23, 2018, 6:56pm UTC](https://discourse.julialang.org/t/julia-version-of-matlab-function-interp1/8540/3 "2018-01-23T18:56:14Z")

</div>

If you want to use Interpolations.jl (a pure julia implementation), but stick with a familiar syntax you can create a reusable wrapper. I’m sure this could be improved, but here is an example that contains the same basic functionality as the matlab version including extrapolation options. Note that Interpolations has way many more options for spline degree, boundary conditions, and grid representation if you want to customize further.

```julia
using Interpolations

function interp1(xpt, ypt, x; method="linear", extrapvalue=nothing)

    if extrapvalue == nothing
        y = zeros(x)
        idx = trues(x)
    else
        y = extrapvalue*ones(x)
        idx = (x .>= xpt[1]) .& (x .<= xpt[end])
    end
    
    if method == "linear"
        intf = interpolate((xpt,), ypt, Gridded(Linear()))
        y[idx] = intf[x[idx]]

    elseif method == "cubic"
        itp = interpolate(ypt, BSpline(Cubic(Natural())), OnGrid())
        intf = scale(itp, xpt)
        y[idx] = [intf[xi] for xi in x[idx]]
    end
    
    return y
end

```

---

_[View the full topic](https://discourse.julialang.org/t/julia-version-of-matlab-function-interp1/8540)._
