Hi all,
I am using eval function but even if I put semicolon ";" at the end of the line Matlab shows the output in the command window.
Is it possible to not display the result in the command window?

 Respuesta aceptada

OCDER
OCDER el 5 de Sept. de 2018
Editada: OCDER el 5 de Sept. de 2018

1 voto

"I am using eval function but"
Don't use eval. It's one of those functions that's there but not to be used unless absolutely necessary, which is almost never. eval creates very inefficient, hard-to-debug codes.
For your case, use fprintf or disp to display variables you want. Otherwise, use the ";" to silence the output.
A = rand(3) %creates and shows A
A = rand(3); %silenced output for A
disp(A); %shows A
I am guessing you did something like this:
eval('A = rand(3)'); %Does not silence output for A = rand(3)
eval('A = rand(3);') %Silences output A = rand(3). But why bother using eval???

5 comentarios

Miroslav Mitev
Miroslav Mitev el 6 de Sept. de 2018
Editada: Miroslav Mitev el 6 de Sept. de 2018
Thanks that helped. I am using eval to change the name of a variable in each iteration, something like that:
number=['1' '2' '3' '4' '5' '6'];
for i=1:6
b=1:i;
eval(['A' number(i) '=b;'])
end
Is it possible to do that avoiding eval?
Stephen23
Stephen23 el 6 de Sept. de 2018
Editada: Stephen23 el 6 de Sept. de 2018
@Miroslav Mitev: that is exactly what the MATLAB documentation recommends to avoid, and it also tells you what the preferred alternative its: "A frequent use of the eval function is to create sets of variables such as A1, A2, ..., An, but this approach does not use the array processing power of MATLAB and is not recommended. The preferred method is to store related data in a single array"
Simply use indexing to store your data in a single array, which might be numeric, cell, struct, table, etc. Indexing is very efficient, unlike what you are trying to do. Read more here:
Miroslav Mitev
Miroslav Mitev el 6 de Sept. de 2018
@Stephen Cobeldick: Thank you about that. It was very helpful.
WinCento99
WinCento99 el 2 de Nov. de 2020
Editada: WinCento99 el 2 de Nov. de 2020
hey OCDER, can you suggest how to replace eval or something else?
%% Initial structuring
y = [1 2] ;
f = @(y) [y bsxfun(@minus,sin(y(end)),y(end-1))] ;
%% Direct loop
for i = 1:19
y = f(y) ;
end
%% Loop with anonymous function --
% How can I have y after the loop with the true output?
fun = @(x) eval(['for i = 1:' num2str(x) '; y = f(y); end;']);
fun(19)
How can I have y after the loop with the true output?
Steven Lord
Steven Lord el 2 de Nov. de 2020
What you've written will not work for a couple different reasons, the first of which is that it attempts to create the variable i in the anonymous function's workspace.
Not every piece of code is suitable for conversion into an anonymous function. I would define your fun as a regular old function.
function y = fun(f, y, x)
for iter = 1:x
y = f(y);
end
end
If you nested this inside the function where you wanted to call it, you could share f and y with that workspace and avoid having to pass them into fun.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Variables en Centro de ayuda y File Exchange.

Preguntada:

el 5 de Sept. de 2018

Comentada:

el 2 de Nov. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by