Skip to Main Content Skip to Search
Product Documentation

Generating Code and Exporting Fits to the Workspace

Generating Code from the Curve Fitting Tool

You can generate and use MATLAB code from an interactive session in the Curve Fitting Tool. 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 all fits and plots in your Curve Fitting Tool session follow these steps:

  1. Select File > Generate Code.

    The Curve Fitting Tool generates code from your session and displays the file in the MATLAB Editor. The file includes all fits and plots in your current session. The file captures the following information:

    • Names of fits and their variables

    • Fit settings and options

    • Plots

    • Curve and surface fitting objects and methods used to create the fits:

      • A cell-array of cfit or sfit objects representing the fits

      • A structure array with goodness-of fit information.

  2. Save the file.

To recreate your fits and 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:

[fitresult, gof] = myFileName(a, b, c)

where a, b, and c are your variable names, and myFileName is the file name.

Calling the file from the command line does not recreate your Curve Fitting Tool GUI and session. When you call the file, you get the same plots you had in your Curve Fitting Tool session in standard MATLAB figure windows. There is one window for each fit. For example, if your fit in the Curve Fitting Tool session displayed main, residual and contour plots, all three plots appear in a single figure window.

Curve Fitting Objects and Methods

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.

cfit and sfit objects are a subclass of fittype objects, so they inherit all the same methods of fittype and provide additional methods. For a list of available methods, see Curve and Surface Fitting Objects and Methods.

See Function Reference for information on all Curve Fitting Toolbox functions, classes, and methods.

Exporting a Fit to the Workspace

To export a fit to the MATLAB workspace, follow these steps:

  1. Select a fit and save it to the MATLAB workspace using one of these methods:

    • Right-click the fit listed in the Table of Fits and select Save Fit myfitname to Workspace

    • Select a fit figure in the Curve Fitting Tool and select Fit > Save to Workspace.

    The Save Fit to MATLAB Workspace dialog box opens.

  2. Edit the names as appropriate. If you previously exported fits, the toolbox automatically adds a numbered suffix to the default names so there is no danger of overwriting them.

  3. Choose which options you want to export by selecting the check boxes. Check box options are as follows:

    • Save fit to MATLAB object named fittedmodel — This option creates a cfit or sfit object, that encapsulates the result of fitting a curve or surface to data. You can examine the fit coefficients at the command line, for example:

       fittedmodel
          Linear model Poly22:
             fittedmodel1(x,y) = p00 + p10*x + p01*y + p20*x^2...
                                 + p11*x*y + p02*y^2
           Coefficients (with 95% confidence bounds):
             p00 =       302.1  (247.3, 356.8)
             p10 =       -1395  (-1751, -1039)
             p01 =     0.03525  (0.01899, 0.05151)
             p20 =        1696  (1099, 2293)
             p11 =     -0.1119  (-0.1624, -0.06134)
             p02 =   2.36e-006  (-8.72e-007, 5.593e-006)

      You also can treat the cfit or sfit object as a function to make predictions or evaluate the fit at values of X (or X and Y). See the cfit and sfit reference page.

    • Save goodness of fit to MATLAB struct named goodness — This option creates a structure array that contains statistical information about the fit, for example:

      goodness = 
                 sse: 0.0234
             rsquare: 0.9369
                 dfe: 128
          adjrsquare: 0.9345
                rmse: 0.0135
    • Save fit output to MATLAB struct named output — This option creates a structure array that contains information such as numbers of observations and parameters, residuals, and so on. For example:

      output = 
              numobs: 134
            numparam: 6
           residuals: [134x1 double]
            Jacobian: [134x6 double]
            exitflag: 1
           algorithm: 'QR factorization and solve'
          iterations: 1

        Note   Goodness of fit and Output arrays are outputs of the fit function. See the fit reference page.

  4. 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 Your Best Fit in the Workspace. For a list of functions, see Fit Postprocessing. For more information on working with curve fitting objects and methods at the command line, see Curve and Surface Fitting Objects and Methods.

  


 © 1984-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS