# Error using str2func Error: This statement is incomplete

4 views (last 30 days)
kors Atsua on 31 Jul 2019
Edited: kors Atsua on 31 Jul 2019
Hi all,
I am trying to implement a matlab script that builds an in-text recursive function funct based on the number of input n. However, str2func in matlab throws an error like so
" Error using str2func
Error: This statement is incomplete"
at the line:
funct{m} = str2func(strcat(strt,res,enstr));
when n >= 3. Please can some help out here. The full script is below. I know something is off about the loop but I just cant put my finger on what exactly I am doing wrong.Thanks alot.
n = 3;
funct = cell(n,1);
funct{n} = @(x,d,k,g,v,funct) exp(1i*kz(n)*x);
funct{n-1} = @(x,d,k,g,v,funct) exp(1i*kz(n-1)*x)./(1-r(n-1,2)*...
r(n-1,1)*exp(2*1i*kz(n-1)*x));
for m = n-2:-1:1
strt = strcat('@(x,d,k,g,v,funct) exp(1i*kz(',num2str(m),...
')*x)/(1-r(',num2str(m),',1)*r(',num2str(m),',2)*exp(2i*kz(',...
num2str(m),')*x) - r(',num2str(m),',1)*exp(2i*kz(',num2str(m),...
')*x)*(');
res = '';
for l = m+1:n-1
prodp = '';
for p = m:l-1
prodp = strcat(prodp, '(tFPcal(',num2str(p+1),...
')*exp(1i*kz(' ,num2str(p+1),')*d(',num2str(p+1),...
'))*...t(',num2str(p),',2)*t(',num2str(p+1),',1)',')*');
end
prodp = prodp(1:length(prodp)-1);
res = strcat(res, 'r(',num2str(l),',2)*(', prodp, ')+');
end
res = res(1:length(res)-1); enstr = '))';
funct{m} = str2func(strcat(strt,res,enstr));
end
%% Output handling
output1 = funct

Guillaume on 31 Jul 2019
So, instead of writing a program to solve a problem, you are writing a program that writes a program that solves a problem. As a result, you get no help from the editor in finding out the problems with the program that your program creates, since you can't see that code in editor.
It's never a good idea to do this, it's the same reason we say don't use eval. You not only have to debug your program generator, but you have to ensure that he generated program is also correct. More than double the work!
The expression that your program generates is very hard to read. I haven't bothered to try to understand it. At the very least, I'd use sprintf to make it easier to read, e.g.
strt = sprintf('@(x,d,k,g,v,funct) exp(1i*kz(%1\$d)*x)/(1-r(%1\$d,1)*r(%1\$d,2)*exp(2i*kz(%1\$d)*x) - r(%1\$d,1)*exp(2i*kz(%1\$d)*x)*(', m)
%...
Even then, good luck checking for mismatched brackets or basic bugs.
It's also unclear why funct is an input to any of the anonymous functions since it's never used by any of them. For that matter, it doesn't appear that d, k, g, and v are used by the anonymous functions. Only x. And if kz and r are variables, not functions, the value of these inside the function will be the value of the variables when the anonymous functions are created, not the values when the functions are used. I'm not at all convinced that any of the functions you've created do what you want
Anonymous functions are only useful for simple one-liner code. Ignoring the fact that they're closure, anonymous functions are just the same as plain functions (m file). So, if the code gets complex just write a plain function where you can use things like loops. If you do need the closure property you can use nested functions instead.
It looks like the code you're trying to generate could easily be written as a loop in a plain or nested function, or perhaps with recursion, so I would urge you to abandon this metaprograming and switch to using loops.