Programming and Data Types | ![]() ![]() |
Saving and Loading Function Handles
You can use the MATLAB save
and load
functions to save function handles to MAT files, and then load them back into your MATLAB workspace later on. This example shows an array of function handles saved to the file, savefile
, and then restored.
fh_array = [@sin @cos @tan]; save savefile fh_array; clear load savefile whos Name Size Bytes Class fh_array 1x3 48 function_handle array Grand total is 3 elements using 48 bytes
Possible Effects of Changes Made Between Save and Load
If you load a function handle that you saved in an earlier MATLAB session, the following conditions could cause unexpected behavior:
save
was done. The function handle you just loaded doesn't know about the new methods.
In the first two cases, the function handle is now invalid, since it no longer maps to any existing source code. Although the handle is invalid, MATLAB still performs the load successfully and without displaying a warning. An attempt to evaluate the handle however results in an error.
![]() | Testing for Data Type | Handling Error Conditions | ![]() |