howto multiline error msg

Hi guys,
i m trying to do sth like:
error('very long error msg...
bla bla bla')
This does not work. Neither this:
error('very long error msg',...
'bla bla bla')
Is there any way it works? thx...

2 件のコメント

Timon Viola
Timon Viola 2020 年 3 月 24 日
In MATLAB 2019a (and most likely above that) the following works:
error('foo:bar','My long error\n message')
and
warning('foo:bar','My long error\n message')
gives the following:
This questions still gets a fair ammount of views, so it might worth clearing it up. (And I am glad that there is no need for the "ellipsis (...) " syntax....
DadPunsAreBadPuns
DadPunsAreBadPuns 2020 年 7 月 17 日
Perhaps this is a 'cleaner' solution?
error_message = [ 'Expected datetime data entries.\n' , ...
'Could not find datetime entries.\n' , ...
'What gives, Kevin?!' ];
error( 'u:stuffed:it' , error_message );
Or just combine it into one command. Your choice.
error( 'u:stuffed:it' , [ 'Expected datetime data entries.\n' , ...
'Could not find datetime entries.\n' , ...
'What gives, Kevin?!' ];
FTR: my name is not 'Kevin.' ;-)

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

回答 (4 件)

Stephen23
Stephen23 2017 年 2 月 3 日
編集済み: Stephen23 2017 年 2 月 3 日

4 投票

It is written quite clearly in the error help that to throw "...a formatted error message with a line break. You must specify more than one input argument with error if you want MATLAB to convert special characters (such as \n) in the error message"
So this is clearly not going to work:
error('Hello\nWorld.')
but this will:
error('Hello\n%s','World')
Try it. It works. It is explained in the documentation.

1 件のコメント

Hannes Helmholz
Hannes Helmholz 2020 年 5 月 13 日
Thanks to you for the answer. Curses to Mathworks for this syntax...

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

Walter Roberson
Walter Roberson 2014 年 3 月 19 日

3 投票

Try
error(sprintf('very long error msg\nbla blah bla'))

6 件のコメント

Matt J
Matt J 2017 年 1 月 27 日
編集済み: Matt J 2017 年 1 月 27 日
oxy Replied:
hi walter,
it does not work as well. Try:
error(sprintf('very long error msg',...
'bla blah bla'))
I was not talking about the '\n's but about spliting the code lines themselves in multiple lines.
Walter Roberson
Walter Roberson 2017 年 1 月 27 日
error(['very long error msg',...
'bla bla bla'])
Walter Roberson
Walter Roberson 2017 年 1 月 27 日
If you have R2016b or later, you can use
error("very long error msg" + ...
"bla bla bla")
Richard Crozier
Richard Crozier 2017 年 2 月 3 日
I was intrigued by this and tried it in R2016b, but I get:
>> error("very long error msg" + ...
"bla bla bla")
error("very long error msg" + ...
Error: Creating a string using double quotes is not supported. Use the string function.
Walter Roberson
Walter Roberson 2017 年 2 月 3 日
Ah, forgot that is from an engineering test version, sorry.
Raymond MacNeil
Raymond MacNeil 2019 年 11 月 22 日
I can confirm that Walter's code works in 2018b.

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

Steven Lord
Steven Lord 2017 年 2 月 3 日

1 投票

You can concatenate two char vectors together using square brackets. You can include a break in the middle of that concatenation operation using an ellipsis (...) like this:
disp(['The cake is ', ...
'a lie!'])
Robert Cumming
Robert Cumming 2014 年 3 月 19 日

0 投票

error('ErrorTAG:TagName', strcat ( 'very long error msg ', ...
'bla bla bla') )

2 件のコメント

Salaheddin Hosseinzadeh
Salaheddin Hosseinzadeh 2014 年 3 月 19 日
How did you figure this out?
Matt J
Matt J 2017 年 1 月 27 日
oxy Commented:
Hi Robert,
thx! I just wander why it has to be so complicated!? This is a code that works on octave:
error("very long error msg \
which needs many lines")
Any way, even if i wont be able to learn the solution by heart, there is one :-)
thx a lot

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

カテゴリ

質問済み:

oxy
2014 年 3 月 19 日

コメント済み:

2020 年 7 月 17 日

Community Treasure Hunt

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

Start Hunting!

Translated by