Borrar filtros
Borrar filtros

str2func vectorize input x1, x2 to x

2 visualizaciones (últimos 30 días)
Qun Wu
Qun Wu el 16 de Mzo. de 2017
Respondida: Qun Wu el 16 de Mzo. de 2017
When using str2func, I find that I can do these things such as,
str = 'x(1)^2 + 3*x(2)';
f = str2func(['@(x)' str]);
f([4 5])
and
str1 = 'x + 2*u';
f1 = str2func(['@(x,u)' str1]);
f1(2,3)
However, what I need is something like
str2 = 'x(1) + u - x(2)';
f2 = str2func('@(x,u)', str2);
f2([1,2],3)
which gives me an error "Too many input arguments."
Any suggestions please? Thank you all in advance.
Qun

Respuesta aceptada

dpb
dpb el 16 de Mzo. de 2017
You didn't follow your previous successful pattern...
f1 = str2func(['@(x,u)' str1]);
as written is the concatenation of two strings; you correctly concatenated the argument list to the function in the previous cases but for some reason didn't for the following...
f2 = str2func('@(x,u)', str2);
Simply write like you did f1
f2 = str2func(['@(x,u)' str2]);
and all is well. The error is there were two instead of only one string in the argument list.

Más respuestas (2)

John BG
John BG el 16 de Mzo. de 2017
Editada: John BG el 16 de Mzo. de 2017
Hi Qun Wu
with the following syntax it works ok
str2 = 'x1 + u - x2'; f2 = str2func(['@(x1,x2,u)', str2]); f2(1,2,3)
the following also works ok
str2 = 'x(1) + u - x(2)'; f2 = str2func(['@(x,u)', str2]);f2([1 2],3)
if you find this answer useful would you please be so kind to mark my answer as Accepted Answer?
To any other reader, please if you find this answer
please click on the thumbs-up vote link
thanks in advance
John BG

Qun Wu
Qun Wu el 16 de Mzo. de 2017
You guys are the best.

Categorías

Más información sobre MATLAB en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by