Products & Services Solutions Academia Support User Community Company

Learn more about Curve Fitting Toolbox   

Generating M-Files and Exporting Fits to the Workspace

Introducing Programmatic Surface Fitting

Surface Fitting Tool makes it easy to plot and analyze fits at the command line. You can export individual fits to the workspace for further analysis, or you can generate an M-file to recreate all fits and plots in your session. By generating M-files you can use your interactive surface fitting session to quickly assemble code for surface fits and plots into useful programs.

For programmatic surface fitting examples, see Programmatic Surface Fitting.

Generating M-Files from the Surface Fitting Tool

You can generate and use MATLAB code from an interactive session in the Surface 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 M-file without modification, or you can edit and customize the M-file as needed.

To generate an M-file for all fits and plots in your Surface Fitting Tool session follow these steps:

  1. Select File > Generate M-File.

    The Surface Fitting Tool creates an M-file from your session and displays the file in the MATLAB Editor. The file includes all fits and plots in your current session. The M-file captures the following information:

    • Names of fits and their variables

    • Fit settings and options

    • Plots

    • Surface fitting objects and methods used to create the fits:

      • A cell-array of sfit objects representing the fits

      • A structure array with goodness-of fit information.

  2. Save the M-file.

To recreate your fits and plots, call the M-file from the command line with your original data as input arguments. You also can call the M-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 M-file name.

Calling the M-file from the command line does not recreate your Surface Fitting Tool GUI and session. When you call the M-file, you get the same plots you had in your Surface Fitting Tool session in standard MATLAB figure windows. There is one window for each fit. For example, if your fit in the Surface Fitting Tool session displayed surface, residual and contour plots, all three plots appear in a single figure window. The following example shows a fit figure with surface and residual plots and some removed outliers.

For more information on working with surface fitting objects and methods, see Programmatic Surface Fitting.

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:

    • Select a fit tab in the Surface Fit Tool and select Fit > Save to Workspace.

    • Right-click the fit listed in the Table of Fits and select Save Fit myfitname 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 an sfit object, that encapsulates the result of fitting a 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 sfit object as a function to make predictions or evaluate the surface at values of X and Y. See the 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.

For more information on working with surface fitting objects and methods at the command line, see Programmatic Surface Fitting.

  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

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