2017-06-04 98 views
1

我试图打印一条有点长的警告消息,其中包含2个变量调用。这里是我的代码:通过多行打印警告信息

warning('MATLAB:questionable_argument', ... 
      'the arguments dt (%d) and h (%d) are sub-optimal. Consider increasing nt or decreasing nx.', ... 
      dt, h) 

很显然,在查看MATLAB代码时,文本行向右延伸。我怎样才能打破它,所以它很好地包装?我已经尝试了很多东西,但不断收到语法错误。

+1

在字符串中插入'\ n'? (不知道如果我正确的问题) –

回答

1

正如评论中所建议的,只需插入一个\n您想要打破此行的位置。你也可以使用一个变量的文本,以方便在代码中还写着:

txt = sprintf(['the arguments dt (%d) and h (%d) are sub-optimal.\n'... 
    'Consider increasing nt or decreasing nx.'],dt,h); 
warning('MATLAB:questionable_argument',txt) 
1

更简单的版本比EBH已经提供的是如下所示:

str1 = 'text 1'; 
str2 = 'text 2'; 
str3 = 'etc.'; 
str = sprintf('\n%s \n%s \n%s \n',str1,str2,str3); 
warning(str) 
+2

哪一部分是“更简单”? –

0

如果你只是嵌入转义字符,如警告字符串\n,它不会工作:

warning('Hi there.\nPlease do not do that.') 

将只打印出:

警告:您好\ n请不要那样做

不过,如果你预先格式化使用sprintf文本,然后所有的转义字符会工作。例如:

warnText = sprintf('Hi there.\nPlease do not do that.'); 
warning(warnText) 

可生产你想要什么:

警告:您好。
请不要这样做。