Using Python with the MATLAB engine, how can I supress the warning

I'm using Python to fetch some block's mask parameter. Those parameter are string with no evaluation of value needed.
Every time I try to load the model, I receive those warning:
Warning: Error evaluating 'PreLoadFcn' callback of block_diagram 'LibraryTest'.
Callback string is 'LibraryTest_init;'
> In load_system>i_load_system (line 44)
In load_system (line 20)
Warning: Unrecognized function or variable 'LibraryTest_init'.
> In load_system>i_load_system (line 44)
The Python script is not in the model folder, as such, those warning are expected as the init file is with the model.
Is there a way to just supress those warning in python so that they don't print?

4 Comments

If the warnings are in the Matlab Command Window. You can use this below warnings off command to supress it.
The warning are in the python script console. When I try to turn off the warning, I have an error:
only a scalar struct can be returned from MATLAB
When trying to turn off all warning.
My code look like this:
eng = matlab.engine.start_matlab()
try:
WarningStatus = eng.warning # <matlab.engine.matlabengine.MatlabFunc object>
eng.warning('off', 'all') # To turn off all warning
eng.load_system(ModelPath, nargout=0) # Open the wanted model. That's where I want to suppress the warning
[ DO STUFF ]
finally:
eng.warning(WarningStatus) # Restore warning
eng.quit() # Close the engine
Use nargout = 0 to explicitly tell Python not to expect a return value.
eng = matlab.engine.start_matlab()
try:
% 1. Save the current warning state directly into the MATLAB workspace.
eng.eval("previousState = warning;", nargout=0)
% 2. Turn off warnings.
eng.warning('off', 'all', nargout=0)
% Rest of the Code
finally:
% 3. Restore the warning state
if eng.workspace.get('previousState'):
eng.eval("warning(previousState);", nargout=0)
% Rest of the Code
Thanks you! That fixed the problem. The only problem left is that the line
if eng.workspace.get('previousState'):
give the error: 'MatlabWorkSpace' object has no attribute 'get'
I tried:
if eng.workspace.get('previousState'): % Give the error: 'MatlabWorkSpace' object has no attribute 'get'
if "previousState" in eng.worspace: % Give the error: Variable name must be string, not int
if eng.exist('previousState', 'var'): % Still print some information. If set with nargout = 0, return nothing so not usefull
if eng.workspace["previousState"] != []: % Give the error: only a scalar struct can be returned from MATLAB
So I simply used the basic try/except:
try:
eng.eval("warning(previousState);", nargout=0)
except e:
warnings.warn(f"Error while restoring the previous warning state: \n{e}")
Considering that it is in the same script and same function, if the variable does not exist, there is another problem.

Sign in to comment.

Answers (0)

Products

Asked:

on 6 Jan 2026

Edited:

on 9 Jan 2026

Community Treasure Hunt

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

Start Hunting!