Error creating a function handle using str2func in a function
4 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Philip Borghesani
2011-1-21
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'
更多回答(3 个)
Kenneth Eaton
2011-1-21
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 个评论
Walter Roberson
2011-1-21
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
2011-1-20
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...
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Platform and License 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!