我在这里先向您的帮助表示感谢。通常在matlab中使用...允许一个人在编辑器中换行。但是当我输入这样的东西时
error('Really long error message ... that I would like to wrap');
“我想要包装的”部分失去了它作为字符串的身份。是否可以在matlab中包装这样的代码,如果是这样,我该怎么做?
编辑:天真的解决方案是将字符串分解为几个字符串,连接它们,并将结果保存为变量。我想要一个更清洁的解决方案。
好吧,如果最后字符串应该是一行,你可以打破字符串,而不必将字符串除以给定的数字。您根本不需要进行任何计算,每条线的长度(或字符数)不必等于下一行。
这是一个没有错误运行的例子(mmmh ......显然不是那个!):
error([
'Really long error message ' ...
'that I would like to wrap ' ...
'but each line in the editor doesn''t need to be the same lenght ' ...
'because in the end it will be concatenated ' ...
'in a single ' ...
'line. The only thing that matters is to put a '' character at the beginning, and ' ...
' a ''... sequence of character at the end of each line'
] );
虽然它不是很优雅,但...
的唯一额外要求是在每行的起始端添加'
符号。线条不需要一致,你可以随意打破它们。
方法1:我认为你可以尝试一种单元阵列方法,如果它适合你的清洁方式,虽然我猜它在概念上类似于@Hoki的方法 -
emsg = [{'Really long error message that we are trying really really '} ...
{'hard to fit into one line, because if we do not, then we are '} ...
{'doomed.'}]
error(horzcat(emsg{:}))
方法2:另一种方法可以考虑使用strcat
,这可能是一个微小的清洁和直接(至少与细胞阵列方法相比) -
msg = strcat( ...
'Really long error message that we are trying really really ', ...
'hard to fit into one line, because if we do not, then we are ', ...
'doomed.');
error(msg)