generate code and export fits to the workspace
generating code from the curve fitter app
you can generate and use matlab® code from an interactive session in the curve fitter app. in this way, you can transform your interactive analysis into reusable functions for batch processing of multiple data sets. you can use the generated file without modification, or you can edit and customize the file as needed.
to generate code for the currently selected fit and its opened plots in your curve fitter app session, follow these steps:
on the curve fitter tab, in the export section, click export and select generate code.
the curve fitter app generates code from your session and displays the file in the matlab editor. the file includes the currently selected fit in your session and its opened plots. the file captures the following information:
name of the fit and its variables
fit settings and options
plots
curve or surface fitting object and the function used to create the fit:
cfit
orsfit
object representing the fita structure with goodness-of-fit information
save the file.
to recreate your fit and its plots, call the file from the command line with your original data as input arguments. you also can call the file with new data.
for example, enter the following code, where a
,
b
, and c
are the variable names and
myfilename
is the file
name.
[fitresult,gof] = myfilename(a,b,c)
calling the file from the command line does not recreate your curve fitter app session. when you call the file, you get the same plots you had in your curve fitter app session in a standard matlab figure window. for example, if your fit in the curve fitter app session displayed fit, residuals, and contour plots, all three plots appear in a single figure window.
curve fitting functions
the curve and surface fit objects (cfit
and
sfit
) store the results from a fitting operation, making
it easy to plot and analyze fits at the command line.
to learn about available functions for working with fits, see .
exporting a fit to the workspace
to export a fit to the matlab workspace, follow these steps:
select a fit and save it to the matlab workspace using one of these methods:
right-click the fit in the table of fits pane, and select save "
myfitname
" to workspace.on the curve fitter tab, in the export section, click export and select export to workspace.
the app opens the save fit to matlab workspace dialog box.
edit the names as appropriate. if you previously exported fits, the app automatically adds a numbered suffix to the default names to avoid overwriting existing names.
choose which options you want to export by selecting the check boxes. check box options are as follows:
save fit to matlab object named — this option creates a
cfit
orsfit
object, that encapsulates the result of fitting a curve or surface to data. you can examine the fit coefficients at the command line.fittedmodel
you also can treat thelinear model poly22: fittedmodel(x,y) = p00 p10*x p01*y p20*x^2 p11*x*y p02*y^2 where x is normalized by mean 1982 and std 868.6 and where y is normalized by mean 0.4972 and std 0.2897 coefficients (with 95% confidence bounds): p00 = 0.4227 (0.3837, 0.4616) p10 = -0.137 (-0.1579, -0.1161) p01 = -0.1913 (-0.2122, -0.1705) p20 = 0.0234 (-2.895e-05, 0.04682) p11 = 0.067 (0.04613, 0.08787) p02 = -0.02946 (-0.05288, -0.006043)
cfit
orsfit
object as a function to make predictions or evaluate the fit at values of x (or x and y). see and .save goodness of fit to matlab struct named — this option creates a structure that contains statistical information about the fit.
goodness
goodness = struct with fields: sse: 9.4302 rsquare: 0.6565 dfe: 287 adjrsquare: 0.6505 rmse: 0.1813
save fit output to matlab struct named — this option creates a structure that contains information such as number of observations, number of parameters, residuals, and so on.
output
output = struct with fields: numobs: 293 numparam: 6 residuals: [293×1 double] jacobian: [293×6 double] exitflag: 1 algorithm: 'qr factorization and solve' iterations: 1
note
the goodness-of-fit and output structures are outputs of the
fit
function.
click ok to save the fit options to the workspace.
after you save your fit to the workspace, you can use fit postprocessing functions. for an example, see analyzing best fit in the workspace. for more information and a list of functions, see fit postprocessing.