Problem saving/loading structure with function handles

조회 수: 11 (최근 30일)
Joseph
Joseph 2011년 1월 24일
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
Paulo Silva
Paulo Silva 2011년 1월 24일
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'

댓글을 달려면 로그인하십시오.

채택된 답변

Kenneth Eaton
Kenneth Eaton 2011년 1월 24일
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
Joseph
Joseph 2011년 1월 24일
Thanks again for the help. I added in a line 'addpath(genpath('Database Files'))' that will set my path for all subfolders.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Bruno Luong
Bruno Luong 2011년 1월 24일
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

카테고리

Help CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by