Interp1 returns NaN for a value (2024)

312 views (last 30 days)

Show older comments

Matthew Hunt on 8 Oct 2019

  • Link

    Direct link to this question

    https://www.mathworks.com/matlabcentral/answers/484181-interp1-returns-nan-for-a-value

  • Link

    Direct link to this question

    https://www.mathworks.com/matlabcentral/answers/484181-interp1-returns-nan-for-a-value

Edited: Stephen23 on 21 Nov 2023

I have some data (x,f(x)) where 0<=x<=1 and I have N points. I wish to obtain f for an inteverval [a,b] which is well within the limits of 0 and 1, and on my end point, I get a NaN so some reason. I think my data set goes up from 0.001 to 0.999, and the set I want values for goes between 0.05 and 0.96. There is no extrapolation required.

What's going on exactly?

9 Comments

Show 7 older commentsHide 7 older comments

dpb on 8 Oct 2019

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/484181-interp1-returns-nan-for-a-value#comment_753868

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/484181-interp1-returns-nan-for-a-value#comment_753868

Show us the data and code...we can't guess.

Matthew Hunt on 8 Oct 2019

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/484181-interp1-returns-nan-for-a-value#comment_753903

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/484181-interp1-returns-nan-for-a-value#comment_753903

I can't show you the data, it's proprietry.

The code is:

[SOC_c, index] = unique(SOC_c);

O_plus = interp1(SOC_c, OCV_c(index),SOC_cathode);

[SOC_a, index] = unique(SOC_a);

O_minus = interp1(SOC_a, OCV_a(index),SOC_anode);

dpb on 8 Oct 2019

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/484181-interp1-returns-nan-for-a-value#comment_753914

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/484181-interp1-returns-nan-for-a-value#comment_753914

Matthew Hunt on 8 Oct 2019

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/484181-interp1-returns-nan-for-a-value#comment_753917

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/484181-interp1-returns-nan-for-a-value#comment_753917

I really don't think it is. I think it's a problem with the interp1 routine personally.

If you are unwilling to help. I guess I will have to use something else or write my own routine.

dpb on 8 Oct 2019

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/484181-interp1-returns-nan-for-a-value#comment_753997

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/484181-interp1-returns-nan-for-a-value#comment_753997

It's not that we're "unwilling" but if we can't reproduce an unspecified problem, what are we supposed to do? Buy the Crystal Ball Toolbox?

I be surprised if putting a set of x,y data into a .mat file and attaching it with no identifying info or context could possibly be of any real impact to your organization.

Or, if you wish, contact me directly thru my link thru the ML Contributors link and I'll destroy anything received once the issue is resolved one way or another.

Sciora Chang on 25 Oct 2021

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/484181-interp1-returns-nan-for-a-value#comment_1798738

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/484181-interp1-returns-nan-for-a-value#comment_1798738

Have you solved this problem? I have met the exact same situation and couldn't figure out why.

dpb on 25 Oct 2021

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/484181-interp1-returns-nan-for-a-value#comment_1799428

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/484181-interp1-returns-nan-for-a-value#comment_1799428

As we told the original poster some two years ago, unless you post the data and the code, there's nothing can do beyond the general suggestions given here then -- interp1 has been around long enough that if there were some actual flaw in it, it is highly unlikely it wouldn't have been found long before now.

Attach the data at a minimum...

Michael Demkowicz on 21 Nov 2023

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/484181-interp1-returns-nan-for-a-value#comment_2969877

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/484181-interp1-returns-nan-for-a-value#comment_2969877

  • interpdata.mat

I've run into the same problem: interp1 returns a NaN at the endpoint of the interpolation range. I'm attaching the data to demonstrate the problem. I see NaN when I load the data and run the following commands:

nirrhor=interp1(irr,irrhor,r);

nirrhor(length(r))

Stephen23 on 21 Nov 2023

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/484181-interp1-returns-nan-for-a-value#comment_2970017

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/484181-interp1-returns-nan-for-a-value#comment_2970017

Edited: Stephen23 on 21 Nov 2023

Open in MATLAB Online

  • interpdata.mat

"I've run into the same problem: interp1 returns a NaN at the endpoint of the interpolation range. I'm attaching the data to demonstrate the problem. I see NaN when I load the data and run the following commands"

Lets take a look:

S = load('interpdata.mat')

S = struct with fields:

irr: [10001×1 double] irrhor: [10001×1 double] r: [0 5.1557e-04 0.0010 0.0015 0.0021 0.0026 0.0031 0.0036 0.0041 0.0046 0.0052 0.0057 0.0062 0.0067 0.0072 0.0077 0.0082 0.0088 0.0093 0.0098 0.0103 0.0108 0.0113 … ] (1×11923 double)

V = interp1(S.irr,S.irrhor,S.r)

V = 1×11923

-0.0058 -0.0058 -0.0058 -0.0058 -0.0058 -0.0058 -0.0058 -0.0058 -0.0058 -0.0058 -0.0058 -0.0058 -0.0058 -0.0058 -0.0058 -0.0058 -0.0058 -0.0058 -0.0058 -0.0058 -0.0058 -0.0058 -0.0058 -0.0058 -0.0058 -0.0058 -0.0058 -0.0058 -0.0058 -0.0058

X = find(isnan(V))

X = 11923

fprintf('%.42f %.42f\n', S.r([1,X]), S.irr([1,end])) % requested; supplied

0.000000000000000000000000000000000000000000 6.1466000000000002856381797755602747201919560.000000000000000000000000000000000000000000 6.146599999999999397459760075435042381286621

The last value you requested clearly lies outside the domain of your data. It is unclear what the problem is, everything seems to be working exactly as documented and expected.

Sign in to comment.

Sign in to answer this question.

Answers (2)

Star Strider on 8 Oct 2019

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/484181-interp1-returns-nan-for-a-value#answer_395330

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/484181-interp1-returns-nan-for-a-value#answer_395330

You could have NaN values in your data. If so, first use the fillmissing function (R2016b and later reseases), then interp1.

2 Comments

Show NoneHide None

Matthew Hunt on 8 Oct 2019

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/484181-interp1-returns-nan-for-a-value#comment_753924

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/484181-interp1-returns-nan-for-a-value#comment_753924

I checked and there are no problems with the data. There are some non-unique points which I removed but that shouldn't matter.

Star Strider on 8 Oct 2019

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/484181-interp1-returns-nan-for-a-value#comment_753927

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/484181-interp1-returns-nan-for-a-value#comment_753927

If you are not extrapolating (that should only produce NaN values for the extrapolated values if you do not include a method designation and the 'extrap' argument), and there are no NaN values anywhere in your data, and we can’t see your data to test it, this will likely remain unsolved.

Sign in to comment.

Chris B on 27 Aug 2022

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/484181-interp1-returns-nan-for-a-value#answer_1035635

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/484181-interp1-returns-nan-for-a-value#answer_1035635

Moved: Image Analyst on 27 Aug 2022

Open in MATLAB Online

I ran into an issue where my start and endpoints were NaN. Turns out I was defining my x and xquery values differently. For example, try this code:

x=[500:2:1100]*10^-1;

xq=[500*10^-1 :2*10^-1: 1100*10^-1];

x==xq

x ~= xq because you are comparing floats in this case. Change 10^-1 to 10^1 and you'll get x==xq. So if you're getting NaNs, try making your x and xquery both integers and then dividing to get floats afterward.

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Sign in to answer this question.

See Also

Categories

AI, Data Science, and StatisticsStatistics and Machine Learning ToolboxProbability DistributionsContinuous DistributionsNoncentral t Distribution

Find more on Noncentral t Distribution in Help Center and File Exchange

Tags

  • interp1

Products

  • MATLAB

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

An Error Occurred

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.


Interp1 returns NaN for a value (15)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom(English)

Asia Pacific

Contact your local office

Interp1 returns NaN for a value (2024)
Top Articles
Latest Posts
Article information

Author: Rev. Porsche Oberbrunner

Last Updated:

Views: 5662

Rating: 4.2 / 5 (73 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Rev. Porsche Oberbrunner

Birthday: 1994-06-25

Address: Suite 153 582 Lubowitz Walks, Port Alfredoborough, IN 72879-2838

Phone: +128413562823324

Job: IT Strategist

Hobby: Video gaming, Basketball, Web surfing, Book restoration, Jogging, Shooting, Fishing

Introduction: My name is Rev. Porsche Oberbrunner, I am a zany, graceful, talented, witty, determined, shiny, enchanting person who loves writing and wants to share my knowledge and understanding with you.