MATLAB R2014a中regress函数潜在Bug:变量正常秩合格却报错
Fixing "Subscript indices must either be real positive integers or logicals" Error with
regress in MATLAB R2014a From the test code and error you described, this is almost certainly caused by variable name conflict—you've accidentally overwritten the built-in regress function with a variable of the same name.
Why This Happens
MATLAB lets you reuse function names for variables, but once you do that, the original function gets "hidden" from the workspace. When you try to call regress(Y,X) later, MATLAB treats it as trying to index into a variable named regress instead of calling the stats toolbox function, hence the subscript index error.
How to Verify
Run this command in your MATLAB command window:
which regress
- If the output is a file path pointing to MATLAB's built-in stats toolbox (e.g.,
C:\Program Files\MATLAB\R2014a\toolbox\stats\stats\regress.m), then this isn't the issue. - If it shows something like
regress is a variable., then you've confirmed the conflict.
Step-by-Step Fix
- Clear the conflicting variable from your workspace:
clear regress - Re-run your original test code:
This should now execute without errors, as MATLAB will call the correct built-in function.X = rand([10 3]); Y = rand([10 1]); [B,BINT] = regress(Y,X);
Prevent This in the Future
- Avoid using MATLAB built-in function names as variable names. You can check if a name is already in use with:
exist('regress','var') % Returns 1 if it's a variable, 0 otherwise - If you need a similar name, add a suffix (e.g.,
regress_results) to avoid collisions.
内容的提问来源于stack exchange,提问作者John Hardy
相关产品推荐
相关产品推荐

