string 字符串中的 MATLAB 行延续

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5226491/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 01:01:36  来源:igfitidea点击:

MATLAB line continuation within string

stringmatlab

提问by user 7023

In MATLAB, ...is used to continue a line to the next line. But if I want to continue a long string within quotation, what can I do? ...will be treated as a part of the string itself. Using []is not a perfect solution since in most cases I use sprintf/fprintfto parse a long string like sql query. Using []would be cumbersome. thanks.

在 MATLAB 中,...用于将一行延续到下一行。但是如果我想在引号内继续一个长字符串,我该怎么办?...将被视为字符串本身的一部分。Using[]不是一个完美的解决方案,因为在大多数情况下我sprintf/fprintf用来解析像 sql 查询这样的长字符串。使用起来[]会很麻烦。谢谢。

回答by Jeremiah Willcock

If you put the string in brackets, you can build it in several pieces:

如果将字符串放在括号中,则可以将其构建为多个部分:

s = ['abc' 'def' ... 
     'ghi'];

You can then split that statement into several lines between the strings.

然后,您可以将该语句拆分为字符串之间的多行。

回答by Dan

answer=['You can divide strings '...
    ,'by adding a comma '... 
    ,'(as you probably know one year later).'];

回答by Linda

You can use strcat or horzcat, which gives you somewhat more options than [], including the ability to mix in variables along with the hardcoded values.

您可以使用 strcat 或 horzcat,它为您提供了比 [] 更多的选项,包括将变量与硬编码值混合在一起的能力。