Custom warning message does not show when called within a function

28 ビュー (過去 30 日間)
Pascal Weller
Pascal Weller 2020 年 11 月 4 日
コメント済み: Mario Malic 2020 年 11 月 4 日
Hello,
I have got difficulties to get custom warning messages to pop up in MATLAB's console.
Scenario:
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:
clear;
fd = 'test.h5'
...
foo(fd);
...
Script for function foo():
function foo(fd)
if exist(fd,'file')
warning('''%s'' already exists. Overwriting it.',fd);
...
end
...
end
Btw: When I put a fprintf() or disp() or error() instead of the warning() it will show up in the console.
  3 件のコメント
Steven Lord
Steven Lord 2020 年 11 月 4 日
To probe the warning state:
warning query

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

採用された回答

Luna
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);
else
warning([fd, ' file does not exist.']);
end
end
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 件)

カテゴリ

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by