-3

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')
halukb
  • 1
  • Where do you define `sym2fnc_subs`? Could you explain what you are trying to implement? Why are you assigning `out(a,b)` twice in each iteration? – Daniel Mar 04 '15 at 22:06
  • It's not twice actually, i was just trying different syntaxes. I should have written it plainly. sym2fnc_subs() takes symbolic matrices (having large symbolic elements) and substitutes a number of values to its variables in each iteration, and writes them out. subs() command is very slow, so I'm trying other ways to make it faster. This format is written to be faster in forums, but I cannot solve it.. I hope it's more clear now? [the sym2cell() function converts symbolic function to cell or string, to include in the eval function.] – halukb Mar 05 '15 at 11:15
  • As a result class says that the function "fnc" is a function_handle, but it does not evaluate.. Neither with "feval(fnc,0,0)" or not, as in : "fnc(0,0)".. – halukb Mar 05 '15 at 11:16

1 Answers1

1

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'
SCFrench
  • 8,244
  • 2
  • 31
  • 61
  • Yes, that is because feval(fnc,0,0) does not substitute the values [0,0] to [x,y] and return a number, but it returns the function itself.. I don't know if there is an other logical way to do this.. – halukb Mar 05 '15 at 11:00
  • Your fnc is defined as @(y,x)'x + y', meaning no matter what you pass it for x and y, you are going to get the literal string 'x + y' as a result. I think you want to get it to look like this: @(x,y)subs(M, vars, [x, y]) – SCFrench Mar 05 '15 at 20:31
  • But the code below works fine. It looks like the same thing in the context of an eval line.. What could be wrong with my code ? Noting that the function handle is recognized.. sqr_1 = @(x) x.^2; sqr_1(4) sqr_2 = @(x,y) x.^2+y.^2; sqr_2(4,2) – halukb Mar 06 '15 at 15:35
  • I guess I'm a little confused about what sym2fcn_subs2 is trying to do. if you want an anonymous function that adds its two inputs, that would look like this `@(x,y) x+y`, not `@(x,y)'x+y'` (note the extra single quotes which turn the body from an expression into a string literal). – SCFrench Mar 06 '15 at 20:26
  • Thanks @SCFrench, I figured that out later upon your reply and fixed it. Very simple mistake. It's now working great, more then ten times faster then before ! Let me explain what it does again; Application is for faster evaluation of symbolic functions. You input a matrix having elements of large symbolic functions, a vector of symbolic variables and their values to substitute. It converts matrix elements into function handles and evaluates the handles, much faster then substituting the symbolic elements of the matrix. – halukb Mar 07 '15 at 15:19
  • For a single symbolic function (ones I'm dealing with have upto 20k characters), I could get 60 times as much faster results.. – halukb Mar 07 '15 at 15:19