Eval with multiple lines

5 visualizaciones (últimos 30 días)
Mehmet OZC
Mehmet OZC el 29 de Oct. de 2015
Comentada: Walter Roberson el 29 de Oct. de 2015
I have to run following with eval function in a single line.
a = 1:20; % first step is to define a variable
b = '[nan a(2:end)]'; % then index should be used. This should be in a single line.
c = eval(b);
My proposal is (which is wrong);
>> b = 'a = 1:20; [nan a(2:end)]'; % I need a single line such this
>> c = eval(b)
Error: The expression to the left of the equals sign is not a
valid target for an assignment.
There are some suggestions not to use eval. I have to use. Any help apreciated!

Respuesta aceptada

Walter Roberson
Walter Roberson el 29 de Oct. de 2015
You cannot return a value from eval().
Does the assignment specifically require you to use eval()? We firmly discourage the use of eval().
  4 comentarios
Mehmet OZC
Mehmet OZC el 29 de Oct. de 2015
>> b = 'a = 1:20; [nan a(2:end)]'; % I need a single line such this
>> eval(b)
>> c = ans;
Walter Roberson
Walter Roberson el 29 de Oct. de 2015
Right. Please post the text of the assignment.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Environment and Settings en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by