Is there a way to suppress command outputs to command window?

Hello guys, I know I can use ; in the end of the commands to prevent them from echoing to the command windows. But now, my situation is that I have a very long code, dispersed in so many m files.
Several command in them, on purpose, for sake of reporting the status, (and useful for debugging( do not have semicolons. Now I am running the code in a loop, and I think having all these outputs slows the process.
How can I suppress all command outputs, without manually going through the long code and putting a semicolon on each line?
Bonus question: There are some disp functions as well that report to the command line. can I also suppress them?
Thanks

回答(6 个)

evalc('yourmainfile');

4 个评论

Thanks for the comment. I tried this function, but it does not solve my issue. Maybe because my main file is a GUI form?! Still the command outputs were showing up in the command window.
The callbacks will be evaluated from elsewhere, so yes, this won't work for an app.
What I would recommend doing is taking an hour or two and going through and adding the following around your disp statements:
if isverbose
disp whatever
end
Then you can write a function "isverbose". If it returns 1, the disp is evaluated other wise it returns zero and it isn't. This will give you the flexibility you desire in an elegant way.
Thanks a lot! Worked for me for suppressing command line outputs of a model dependency function:
evalc('[files, ~, ~, ~] = dependencies.fileDependencyAnalysis(ModelName, ''manifestfile'')');

请先登录,再进行评论。

You can create a function in the current directory or top directory of the path called disp.m and put the following single line of code in there:
function thisdoesnothing (varargin)
If you need to use DISP again, just change the name of the m-file to something else (e.g. thisdoesnothing.m)

1 个评论

Thanks a very good solution to suppress the DISP function outputs!
Is it possible that instead of overloading disp function by creating another m-file named disp.m, I create a function named disp inside my main file (main_prog.m), but it gets a GLOBAL scope so that it also will be accessible from subcode_1.m in the run time? (subcode_1.m is called from within the main_prog.m in the run time)

请先登录,再进行评论。

fid1 = fopen('your_file.m');
fid2=fopen('new_file.m','w')
res={};
while ~feof(fid)
line1 =[fgetl(fid) ';'];
res{end+1,1}=line1
fprintf(fid2,'%s \r\n',line1)
end
fclose(fid1);
fclose(fid2);

3 个评论

I am hoping to find a solution that I can activate/deactivate this suppression of outputs anytime I want, without completely modifying the code each time.
If I am running the code for debugging, and development I will let the outputs show up, but when running the code for results, I will disable them.
I didn't change your file, just created another one, you can choose to run the first or second one

请先登录,再进行评论。

If you're using a sufficiently recent release of MATLAB, create a codeIssues object for the file then call fix with the object and the CheckID of the issues you want to fix as inputs.
dbtype myfun129872.m % Note line 2 is missing its semicolon
1 function y = myfun129872(x) 2 y = x.^2
issues = codeIssues('myfun129872.m')
issues =
codeIssues with properties: Date: 03-May-2025 23:51:30 Release: "R2024b" Files: "/users/mss.system.7c99x/myfun129872.m" CodeAnalyzerConfiguration: "active" Issues: [1x10 table] SuppressedIssues: [0x11 table] Issues table preview Location Severity Fixability Description CheckID LineStart LineEnd ColumnStart ColumnEnd FullFilename _______________ ________ __________ _________________________________________________________________________ _______ _________ _______ ___________ _________ _______________________________________ "myfun129872.m" info auto "Add a semicolon after the statement to hide the output (in a function)." NOPRT 2 2 3 3 "/users/mss.system.7c99x/myfun129872.m"
% fix(issues, "NOPRT")
I didn't make the fix call executable in Answers because I wanted to show you the file with the code issue present and didn't want to have to keep replacing the file each time I ran the code. I did run it in a local session of MATLAB, however, and confirmed it added the semicolon.
As a partial solution, you can use favoriteForceSemicolons.m from this File Exchange download,
It is easiest if you install it as a Quick Access button, as recommended in the Desciption section of the submission.
With this, you can open a file in the Matlab Editor and do Select All (Ctrl-A) in a given file. When you apply favoriteForceSemicolons(), it will add semicolons to all relevant lines of code. I don't know if there is a way to automate this process across multiple files, however.

类别

帮助中心File Exchange 中查找有关 Debugging and Analysis 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by