Custom warning message does not show when called within a function

조회 수: 38 (최근 30일)
Pascal Weller
Pascal Weller 2020년 11월 4일
댓글: Mario Malic 2020년 11월 4일
I have got difficulties to get custom warning messages to pop up in MATLAB's console.
I start execution of the main script. At some point it calls a function that is coded in a seperate .m file. From this called function I would like to display a warning. It executes the line where the warning is called but nothing shows up in the console. How do I make this warning to show up? Thanks in advance for your help!
Dummy code:
Main script which is run:
fd = 'test.h5'
Script for function foo():
function foo(fd)
if exist(fd,'file')
warning('''%s'' already exists. Overwriting it.',fd);
Btw: When I put a fprintf() or disp() or error() instead of the warning() it will show up in the console.
  댓글 수: 3
Pascal Weller
Pascal Weller 2020년 11월 4일
Thanks for the quick response. Your prediction was accurate.
>> warning;
All warnings have the state 'off'.
For anyone facing the same issue: Here is how to enable all warnings:
Steven Lord
Steven Lord 2020년 11월 4일
To probe the warning state:
warning query

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

채택된 답변

Luna 2020년 11월 4일
편집: Luna 2020년 11월 4일
I agree with Walter, put that code on command window to get all warnings on:
warning all on;
Be sure that your if statement result is true. I recommend you to try this first:
function foo(fd)
if exist(fd,'file') == 2 % this is about exist function outputs. Check all the output states. if it is 2, that means file exists.
warning('''%s'' already exists. Overwriting it.',fd);
warning([fd, ' file does not exist.']);
Here is the link for exist function documentation:
  댓글 수: 1
Mario Malic
Mario Malic 2020년 11월 4일
It looks like that if expression is true when expression result is nonempty and contains only nonzero elements (logical or real numeric). Otherwise, the expression is false.

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

추가 답변 (0개)


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




Community Treasure Hunt

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

Start Hunting!

Translated by