Delete 'ans' from the function's output

15 ビュー (過去 30 日間)
Mateusz Zagórski
Mateusz Zagórski 2023 年 8 月 2 日
編集済み: Mateusz Zagórski 2023 年 8 月 2 日
I've got an .m file that consists of a function of form:
function [output variables]=name(input variables)
%code
end
When the function ends, it shows ans= ... in the command window. I've got a few variables printed out in the command window right before the function ends, but then when the function ends I get 34 structures that spam Command Window and the user has to scroll up to see the important data.
Is there a way to omit this 'ans' information? (R2023b)

採用された回答

dpb
dpb 2023 年 8 月 2 日
Sure. Follow the function call with the trailing semicolon, ";"
Otherwise, MATLAB will always echo the result; ans is the default variable if you don't assign variable(s) on the LHS of an assignment statement when calling a function that does have return values.
Of course, if you don't assign the output variables to anything and also suppress the echo'ed output with the semicolon, then that the function ran has no effect; it can't return anything that you can then access for later, so you may as well just omit calling it. :)
You should modify your script to something like
Answer42=themagicfunctionoftheuniverse(ofitsinputs);
in place of simply having (or typing at the command line, maybe?)
themagicfunctionoftheuniverse(ofitsinputs);
Then you'll still have the outputs (which is, one presumes, the reason to call the function to begin with), but not have anything echo'ed to the screen.
  1 件のコメント
Mateusz Zagórski
Mateusz Zagórski 2023 年 8 月 2 日
編集済み: Mateusz Zagórski 2023 年 8 月 2 日
Thanks for your long explanation! I had 'Answer42' in my script indeed (what is also the solution but for the GUI part so I accepted this anwer), but the problem was in the manual run of the function that I explained here https://ch.mathworks.com/matlabcentral/answers/2003932-delete-ans-from-the-function-s-output#comment_2836942
Adding was_it_runned_manually_or_using_GUI if statement solved it.
if (GUI_calculations==0) % Prevent from displaying 'ans' in the Command Window while using Main() manually
clearvars;
end

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeEntering Commands についてさらに検索

製品


リリース

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by