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 件のコメント
Oleg Komarov
2011 年 1 月 20 日
Can you post some more code...It's not obvious what you're trying to do.
採用された回答
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!