Error using lsqncommon (line 67)

3 views (last 30 days)
C A
C A on 5 Oct 2021
Answered: Pratik on 17 Apr 2024
I am trying to fit heat maps from biological cells. My code works for most of my cells but a few cells failed due to incorrect fitting parameters. I dont want to change the fitting parameters because they have been optimized for most cells. Can you please tell me how to proceed to the next cell when the code tries to fit the errenenous cells??
Error using lsqncommon (line 67)
The Levenberg-Marquardt algorithm does not handle bound constraints and the trust-region-reflective algorithm requires at least as many
equations as variables; aborting.
Error in lsqcurvefit (line 257)
lsqncommon(funfcn,xCurrent,lb,ub,options,defaultopt,caller,...
Error in FittingClass/FittingFunction (line 569)
x = lsqcurvefit(@Obj.generalizedgaussianfunction,x0,xdata,ydata,Obj.lb,Obj.ub,Obj.FitOptions);
Error in MainClass/CallingFunctionForFit (line 1331)
[iCell{iFrame}]=FitObj.FittingFunction(iCell{iFrame},signal,MaximumPeakNumber);
FYI:
properties (Access = protected)
FitOptions = optimoptions('lsqcurvefit','Display','off');
end
FitObj=FittingClass(Obj.rootdir,[]);
I can't post the code here because it is pretty long. Please tell me how to avoid the cells that gives out the above error.

Answers (1)

Pratik
Pratik on 17 Apr 2024
Hi,
As per my understanding, you're encountering errors when fitting heat maps of biological cells using MATLAB's lsqcurvefit, due to incompatible fitting parameters for a few outlier cells.You're looking for a way to skip the problematic cells and keep the program running uninterrupted for the rest.
A try-catch block can be implemented around the fitting code. This way, when MATLAB encounters an error for certain cells, it can catch the error and skip those problematic cells, proceeding to the next ones without stopping the entire process.
Please refer to the code snippet below:
for i = 1:numberOfCells % Assuming numberOfCells is the total number of cells you are iterating over
try
% Your current fitting process for each cell
[iCell{iFrame}]=FitObj.FittingFunction(iCell{iFrame},signal,MaximumPeakNumber);
catch ME
% Here, you catch the error and decide what to do. For simplicity, we'll just display a message.
fprintf('Skipping cell %d due to fitting error: %s\n', i, ME.message);
continue; % This will skip the rest of the loop and move to the next cell
end
end
Please refer to the documentation of 'try, catch' for more information on executing statements and resulting errors:
I hope this helps!

Categories

Find more on Interpolation in Help Center and File Exchange

Products


Release

R2017a

Community Treasure Hunt

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

Start Hunting!