MATLAB: Warning: Rank deficient, rank = 0, tol = NaN.

curve fittingif statementlsqcurvefitnonlinear regressionnonlinearmodelrank deficientStatistics and Machine Learning Toolbox

Hi Dears,
I'm using lsqcurvefit to fit my data. Sometimes, when I select an initial guess I get the following error:
Warning: Rank deficient, rank = 0, tol = NaN.
My question is not about how to fix that error. Rather, I hope to see how to avoid that fitting process entirely when it detects that error!
I mean, suppose the curve fitting (using lsqcurvefit or any other optimization tool; such as: NonLinearModel.fit, etc) is located in a function that is called via a main m-file. If the previous warning is detected, then the m-file should ignore or bypass that function and proceeds normally without stopping the program on a red error message says: Error using lsqcurvefit (line 251) Function value and YDATA sizes are not equal.
All what I need is like an if-statement. If the initial guess is healthy then please fit the data; otherwise, ignore the curve fitting stage and jump to the next lines in the main m-file.
Could you please guide me in resolving this issue.
Thank you so much

Best Answer

Have a look at
try
catch
end
That way you might be able to put the curve fitting (or other fragile attempts) in the try part and handle the occasional errors in the catch part.
HTH