Matlab: Convert function handle to string preserve parameters -


any variables , values stored in function handle when created lost if convert function handle string , again using func2str , str2func functions.

is there way overcome ?

like

y = 1; fun = @(x) x + y fun_str = func2str(fun);  clear y  fun = eval(fun_str); result = fun(12); % doesn't work 

concrete have measurement method creating data gets fitted (with of fminsearch) against custom functions alignment procedure. store data future analysis.

since data survives longer code, make independent source file. that's why convert function handle string. in string matlab builtin functions allowed called.

now can of course store y in example. wondering if there more elegant solution not need this.

the problem stems matlab storing value of y within workspace local anonymous function while keeping symbol y part of function definition. then, indicated, func2str function converts definition without replacing symbol value. since example indicates want function handle work after clearing variable y (both value , symbol) current workspace, i'd suggest 2 paths forward.

the first create function handle str2func using value of y instead of letting anonymous function hold it:

fun = str2func(['@(x) x + ',num2str(y)]); 

the other write variant of func2str value replacement after converting function string. functions function can used acquire anonymous function's workspace, , regular expressions can used replace symbols literals. here minimal working example:

function str = func2strrep(fun)      %   grab workspace information     info      = functions(fun);     workspace = info.workspace{1};     symbols = fieldnames(workspace);      %   initialize string     str = func2str(fun);      %   replace workspace variables numeric literals     if not(isempty(symbols))          seps = '[\s\+\-\*\/\\\^\:\=\>\<]';         expr = @(s) ['(',seps,'{1})',s,'(',seps,'{1})'];          k = 1:numel(symbols)              symbol = symbols{k};             value  = num2str(workspace.(symbol));              %   check end of string             str = regexprep(str,['(',seps,'{1})',symbol,'$'],['$1',value]);              %   check bulk             stro = regexprep(str,expr(symbol),['$1',value,'$2']);             %             % put in loop repeated replacement since              % 'all' not working thought would.             while not(strcmp(stro,str))                 str  = stro;                 stro = regexprep(str,expr(symbol),['$1',value,'$2']);             end          end     end end 

i'm not talented regular expressions, replacement method may need tweaking (particularly since regexprep didn't replace thought matches repeated symbols). however, first go produces

>> y = 1; >> z = 2; >> yy = 3; >> f = @(x) x + y + y + z + yy; >> func2strrep(f) ans = @(x)x+1+1+2+3 

both approaches rely on num2str and, therefore, scalar values. vector values, num2str variant produces valid array literal in string form required method work.

the second approach works simple variable storage in example. nested handles , closures , the-like require more complicated algorithm symbol-value determination , possibly replacement.


Comments

Popular posts from this blog

Android : Making Listview full screen -

javascript - Parse JSON from the body of the POST -

javascript - Chrome Extension: Interacting with iframe embedded within popup -