Error creating a function handle using str2func in a function
1 view (last 30 days)
Show older comments
I have been having trouble using STR2FUNC to make a function handle for a string name in a function. It works properly if I add a break point and evaluate the same portion of code at the command line.
This problem comes about because I have created a number of different folders and generate functions of the same name in each folder (i.e FreeEnergy-> COg.m,CO2g.m ; Enthalpy-> COg.m, CO2g.m; Entropy-> COg.m, CO2g.m).
Each function each folder has different code, so I am trying to make a function handle to each and store it in a structure. I first switch the directory to the proper directory and use STR2FUNC to generate the function handle.
Example Code (in function file): cdir = pwd; ...
cd([cdir 'FreeEnergy'])
res(i).G=str2func(name);
functions(res(i).G)
Example Output:
function: 'COg'
type: 'simple'
file: ''
However, if I was to highlight "res(i).G=str2func(name); functions(res(i).G)" and evaluate it at the command line then it adds in the proper file path
Example Output:
function: 'COg'
type: 'simple'
file: 'C:Users\Joe\Documents\Matlab\FreeEnergy\COg.m'
The second is the proper output and does not generate unless I evaluate at the command line. I have no idea why this is happening. Is it a bug? If I was to run the same function again then it will also work properly, but involves a lot of time overhead for creating the individual function code from their symbolic variables. Any suggestions would be appreciated.
1 Comment
Oleg Komarov
on 20 Jan 2011
Can you post some more code...It's not obvious what you're trying to do.
Accepted Answer
Philip Borghesani
on 21 Jan 2011
REHASH will work but it is a big hammer. A faster solution is to use exist which will force MATLAB to look on the disk for the file. WHICH will also to the job but EXIST allows a full file path to be specified.
function gencode
f=fopen('newcode.m','w');
fprintf(f,'function newcode\n');
fclose(f);
hf1=@newcode;
exist('newcode','file');
hf2=@newcode;
functions(hf1)
functions(hf2)
delete newcode.m
Output:
>> gencode
ans =
function: 'newcode'
type: 'simple'
file: ''
ans =
function: 'newcode'
type: 'simple'
file: 'h:\temp\newcode.m'
More Answers (3)
Kenneth Eaton
on 21 Jan 2011
The solution I found was to make a call to REHASH after you create each file. It appears that MATLAB needs a chance to update the list of known files before it can properly create a function handle for a newly-added function. Calling REHASH before generating each function handle will give you the proper file path, whether you are using STR2FUNC or EVAL as Walter suggested.
2 Comments
Walter Roberson
on 21 Jan 2011
You are correct, that would be needed when you create a function file. The original question did not mention that the files were being generated inside of Matlab.
Walter Roberson
on 20 Jan 2011
Perhaps cd to where you need to and then,
res(i).G = eval(['@' name]);
I know eval() is not the greatest of tools, but if it works...
See Also
Categories
Find more on Function Handles 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!