MATLAB: How can we automate Curve Fitting Tool (cftool) processes

MATLAB

I use the Curve Fitting Tool in my workflow. Right now my method is semi automated. I define variables as my stretch data, pull them up in cftool, fit with a gaussian fit. I then type in the a1, b1, and c1 variable outputs in the cftool results window and run my program which graphs the derivative and searches for the max value. How would you automate this process? It's about a ten minute process and I have a large amount of samples to run.

Best Answer

The "cftool" function has a feature that allows you to generate MATLAB code that will carry out the same fitting as you have set up in the tool.
This feature is located in the Curve Fitting Tool Window > File > Generate Code.
When you run the code generated by the tool, it will output a "cfit" or "fit" object, depending on what kind of fitting you are doing.
Once you have created this object, you can use the functions "coeffnames" and "coeffvalues" in order to access those a1, b1, and c1 values.
>>coeffvalues(fitresult)
So, to summarize, once you've generated code for your fit a single time, you can then re-run that function as many times as you need and thus automatically extract those values and automate the process.
For more information about generating code from the Curve Fitting Tool, please read the following guides:
https://www.mathworks.com/help/releases/R2017a/curvefit/generating-matlab-code-and-exporting-fits.html
https://www.mathworks.com/help/releases/R2017a/curvefit/generating-code-and-exporting-fits-to-the-workspace.html