Too many inputs to CFIT/SUBSREF.error when using a for loop
13 次查看(过去 30 天)
显示 更早的评论
I have the following code:
image=a(:,:,n);
Isum=sum(image,1);
[Imax,Iloc]=max(Isum);
I=image(:,Iloc);
fit=fit(L,I,'exp1');
coeff=coeffvalues(fit);
coeff=coeff(1,2)
alpha=coeff;
This works well, and I can put in any number to, n, to get the file I need however when I run this in a for loop to access all values of n, (1:20) it tells me there is a "Error using cfit/subsref>iParenthesesReference (line 36) Too many inputs to CFIT/SUBSREF." error The error occurs on line "fit=fit(L,I,'exp1'); "
Any help would be greatly appreciated!
1 个评论
qingyun liu
2018-6-18
hey I have the same problem, and I have struggled for hours. Dont know why, hope somebody can come and answer your question
回答(2 个)
Kensington Hartman
2022-4-4
Hey there! I realize it's been 5 years so you may have moved on from this, but in case anyone else out there has the same question, I've found that it works as long as I clear the variable 'fit' at the end of the loop. So basically:
for i = 1:n
% Do something to get x and y
fit = fit(x,y,fitType);
clearvars fit; %this line clears fit from the active workspace
end
I'm new to Matlab and not very experienced with computers and coding in general, so I don't know why this is necessary, but it seems to work :)
2 个评论
Voss
2022-4-4
The line:
fit = fit(x,y,fitType);
runs the function fit and stores the result in a variable called fit. Then the next time you want to run the fit function, you cannot because fit now refers to the variable fit instead. Clearing the variable allows you to run the fit function again since there is no longer a variable with the same name.
It is best to avoid naming variables with the same names as functions, so you might do this instead:
for i = 1:n
% Do something to get x and y
my_fit = fit(x,y,fitType);
end
i.e., use my_fit as the variable name, and there is no need to clear it each time (it doesn't conflict with the function fit).
Steven Lord
2022-4-4
As long as there is a variable named fit it takes precedence over the function named fit, preventing you from calling the function.
In general you should avoid defining variables with the same name as a function.
plot = 50:60;
plot(1:10) % Does not open a figure and create graphics. This indexes into the plot variable.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Calculus 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!