Indexing error message that makes no sense with cfit

조회 수: 6 (최근 30일)
Kate
Kate 2018년 5월 5일
댓글: Star Strider 2018년 5월 5일
Hi y'all,
I'm trying to fit a polynomial to some data, and I keep on getting an indexing error message which makes no sense. Perhaps one of you can see the forest for the trees. Here's an example of the data:
r =
23.6406 2.8430
16.8598 1.9837
10.8369 1.2979
6.8227 1.0700
15.3950 1.8485
17.8152 2.5149
21.3562 4.4245
23.5249 2.7839
27.0158 1.6553
24.3364 3.1090
21.7304 2.3873
15.4503 1.3488
21.8857 1.9761
28.4438 8.2376
17.5644 7.2797
14.6695 4.7655
24.3061 1.9693
24.2177 3.8884
26.6174 2.3952
26.3180 8.7369
19.6436 5.7631
-4.3673 1.1343
1.9130 0.5210
9.9632 6.3485
16.6363 4.9399
14.4421 3.3374
but when I try and fit it I get an indexing error message which makes no sense:
f=fit(r(:, 1), r(:, 2),'poly2')
Subscript indices must either be real positive integers or logicals.
I don't see how this can be an indexing issue, as I can index to plot the full dataset:
figure, plot(r(:, 1), r(:, 2), '.')
Any ideas? I've even re-started matlab as it does sometimes get buggy when I've had it open for a while, but no dice.
Thanks!

채택된 답변

Star Strider
Star Strider 2018년 5월 5일
My guess is that the error refers to fit, and MATLAB believes it’s an array, not a function. Do you have a previously assigned variable named ‘fit’ that’s ‘overshadowing’ the function name?
Run this from your Command Window or a script:
which fit -all
If the first line in the returned output is:
fit is a variable.
You’ve found the problem. The solution is to name the variable something else.
  댓글 수: 2
Kate
Kate 2018년 5월 5일
Nailed it. Very silly mistake on my part, thanks :)
Star Strider
Star Strider 2018년 5월 5일
As always, my pleasure!
Thanks! (I learned about this from my own very silly mistake.)

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Get Started with Curve Fitting Toolbox에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by