Problem saving/loading structure with function handles

17 views (last 30 days)
Hi all, I have made a bit of code that stores information about species and stores a number of different fields that contain strings, symbolic functions, and function handles in a structure. The function handles are correctly stored and hold the path information, confirmed by using functions(). However, when I try to save the structure to a .MAT file and load it again, all of the function handle path information is gone. How should I save this in order to retain the information. Also, I have not done any changes to the actual function m-files or their folder.
-Joe
'Name' 'C'
'Tints' 3
'MM' 12.0107
'Phase' 'Gas'
'TRange' [200,1000,6000,20000]
'Cp_R' <1x1sym>
'H_RT' <1x1sym>
'S_R' <1x1sym>
'G_RT' <1x1sym>
'Density'[]
'Diameter'[]
'RedTemp'[]
'G' @C_gas
'H' @C_gas
'S' @C_gas
'Cp' @C_gas
>> functions(database(1).G)
ans =
function: 'C_gas'
type: 'simple'
file: 'C:\Users\Joe\Desktop\MATLAB_today\GasFlowMatrix\Database Files\Standard\GibbsFreeEnergy\C_gas.m'
>> save('test','database') >> functions(database(1).G)
ans =
function: 'C_gas'
type: 'simple'
file: 'C:\Users\Joe\Desktop\MATLAB_today\GasFlowMatrix\Database Files\Standard\GibbsFreeEnergy\C_gas.m'
>> load test.mat
>> functions(database(1).G)
ans =
function: 'C_gas'
type: 'simple'
file: ''
  1 Comment
Paulo Silva
Paulo Silva on 24 Jan 2011
I never used the function functions before but I notice a strange behavior, some functions have something in the file line and others don't
functions(@spy)
ans =
function: 'spy'
type: 'simple'
file: 'C:\Program Files (x86)\MATLAB\R2008b\toolbox\matlab\sparfun\spy.m'
functions(@sin)
ans =
function: 'sin'
type: 'simple'
file: ''
functions(@pi)
ans =
function: 'pi'
type: 'simple'
file: 'MATLAB built-in function'

Sign in to comment.

Accepted Answer

Kenneth Eaton
Kenneth Eaton on 24 Jan 2011
In the documentation for Saving and Loading Function Handles, it has this bullet point for conditions that can cause unexpected behavior:
  • You load the function handle into an environment different from that in which it was saved. For example, the source for the function either does not exist or is located in a different folder than on the system on which the handle was saved.
Based on the code sample you supplied for your previous question, I'm guessing that what happened is that you changed to a specific directory that was not on the MATLAB path to create the function handle stored in database(1).G, saved your data to the .MAT file, then tried to load it in a different directory.
I think the solution is to add to the MATLAB path the directories storing the functions you want handles for. Then they should be visible even when that directory is not the current directory, and loading the function handle from the .MAT file should work properly.
  1 Comment
Joseph
Joseph on 24 Jan 2011
Thanks again for the help. I added in a line 'addpath(genpath('Database Files'))' that will set my path for all subfolders.

Sign in to comment.

More Answers (1)

Bruno Luong
Bruno Luong on 24 Jan 2011
In an old related thread below, there is a way to deal with loading/saving function handle: http://www.mathworks.com/matlabcentral/newsreader/view_thread/235926
Bruno

Categories

Find more on File Operations in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!