Reputation: 1
Is it not possible to do such an evaluation?
I create a function using eval (class verifies a function handle is created) Then I use eval to use this function handle. But it does not evaluate, resulting with the function itself. Tried many different ways to write the line. It is as below. Might there be an easy way to do this?
Why am I doing this : I have large symbolic matrices to sustitute. For faster evaluation, I am trying to create functions out of each element. Any suggestions on that?
(using Matlab v.7)
% CODE --------------------------------
function [out]=sym2fnc_subs2(M,vars,val)
for a=1:size(M,1) for b=1:size(M,2)
eval(['fnc=@(',sym2cell(vars,'comma'),') ''',sym2cell(M(a,b)),''';']);
class(fnc)
eval(['feval(@(varargin)fnc(varargin{:}),',sym2cell(sym(val),'comma'),')'])
eval(['feval(fnc,',sym2cell(sym(val),'comma'),')'])
eval(['out(',int2str(a),',',int2str(a),')=feval(fnc,',sym2cell(sym(val),'comma'),')'])
out(a,b)=eval(['fnc(',sym2cell(sym(val),'comma'),')'])
end; end;
function [C]=sym2cell(M,varargin)
n = ndims(M);
for a=1:size(M,1); for b=1:size(M,2);
if nargin==2
if strcmp(varargin{1},'space'); s=' '; end;
if strcmp(varargin{1},'comma'); s=','; end;
if b==size(M,2); C(a,b) = {[char(M(a,b))]};
else; C(a,b) = {[char(M(a,b)),s]}; end;
else; C(a,b) = {char(M(a,b))}; end;
end; end;
if isvector(C); C=cell2mat(C); end;
% RESULT --------------------------------
>> syms x y
>> [out]=sym2fnc_subs(sym('y+x'),[y x],[0 0])
ans =
function_handle
ans =
y+x
ans =
y+x
??? Error using ==> eval (at the last eval which returns a 'char')
Upvotes: -3
Views: 505
Reputation: 8374
I'm getting an error on this line
eval(['out(',int2str(a),',',int2str(a),')=feval(fnc,',sym2cell(sym(val),'comma'),')'])
The argument to eval resolves to
out(1,1)=feval(fnc,0,0)
The call to feval returns a 1-by-5 char array ('x + y'
) which doesn't fit in out(1,1)
, which can hold only one char. You may want to use cell indexing instead, like this:
>> out{1,1} = feval(fnc, 0, 0)
out =
'x + y'
Upvotes: 1