필터 지우기
필터 지우기

EVAL do not display OUTPUT

조회 수: 44 (최근 30일)
Miroslav Mitev
Miroslav Mitev 2018년 9월 5일
댓글: Steven Lord 2020년 11월 2일
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?

채택된 답변

OCDER
OCDER 2018년 9월 5일
편집: OCDER 2018년 9월 5일
"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
WinCento99
WinCento99 2020년 11월 2일
편집: WinCento99 2020년 11월 2일
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 2020년 11월 2일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Variables에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by