Matlab strcat 函数出现空格问题
我正在尝试实现此目标:
strcat('red ', 'yellow ', 'white ')
我期望看到“红黄白”,但是,我在命令输出中看到“redyellowwhite”。需要做什么来确保空格正确连接?提前致谢。
I'm trying to accomplish this:
strcat('red ', 'yellow ', 'white ')
I expected to see "red yellow white", however, I see "redyellowwhite" on the command output. What needs to be done to ensure the spaces are concatenated properly? Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
更多
发布评论
评论(5)
尽管 STRCAT 忽略尾随空格,它仍然保留前导空格。试试这个:
或者,您可以只使用串联语法:
Although STRCAT ignores trailing white space, it still preserves leading white space. Try this:
Alternatively, you can just use the concatenation syntax:
来自 strcat 的 matlab 帮助页面:
“strcat 忽略尾随 ASCII 空白字符,并从输出中省略所有此类字符。ASCII 中的空白字符是空格、换行符、回车符、制表符、垂直制表符或换页符,所有其中从 MATLAB isspace 函数返回 true 响应。使用串联语法 [s1 s2 s3 ...] 保留尾随空格。
From the matlab help page for strcat:
"strcat ignores trailing ASCII white space characters and omits all such characters from the output. White space characters in ASCII are space, newline, carriage return, tab, vertical tab, or form-feed characters, all of which return a true response from the MATLAB isspace function. Use the concatenation syntax [s1 s2 s3 ...] to preserve trailing spaces. strcat does not ignore inputs that are cell arrays of strings. "
事实上,你可以简单地使用空格的ASCII码:32。因此,你可以这样解决问题:
然后你会得到
str = 'red Yellow White'
。In fact, you can simply use the ASCII code of space: 32. So, you can solve the problem like this:
Then you will get
str = 'red yellow white'
.您可以通过将 strcat() 或类似函数放入单元格中来保护尾随空格。
在这个基本示例中不是很有用。但如果您最终对字符串进行“矢量化”操作,那么它会很方便。常规数组串联不会执行 strcat 所做的一对多串联。
将“my”粘贴在单元格中,即使它是单个字符串也会保留空格。请注意,您必须使用 {} 形式而不是调用 cellstr(),它本身会去除尾随空格。
这可能是因为 Matlab 有两种表示字符串列表的形式:作为 cellstr 数组,其中所有空格都很重要,以及作为空白填充的二维 char 数组,每行都被视为字符串,并且忽略尾随空格。 cellstr 形式最类似于 Java 和 C 中的字符串;如果您有许多长度相似的字符串,则 2-D char 形式可以提高内存效率。 Matlab 的字符串操作函数在两种表示上是多态的,有时会表现出这样的差异。像 'foo' 这样的 char 文字是二维 char 形式的退化单字符串情况,Matlab 的函数也如此对待它。
更新:从 Matlab R2019b 左右开始,Matlab 还具有新的
string
数组类型。双引号字符串文字生成string
数组而不是char
数组。如果您改用string
数组,它将解决您的所有问题。You can protect trailing whitespace in strcat() or similar functions by putting it in a cell.
Not very useful in this basic example. But if you end up doing "vectorized" operations on the strings, it's handy. Regular array concatenation doesn't do the 1-to-many concatenation that strcat does.
Sticking 'my ' in a cell even though it's a single string will preserve the whitespace. Note you have to use the {} form instead of calling cellstr(), which will itself strip trailing whitespace.
This is all probably because Matlab has two forms of representing lists of strings: as a cellstr array, where all whitespace is significant, and as a blank-padded 2-dimensional char array, with each row treated as a string, and trailing whitespace ignored. The cellstr form most resembles strings in Java and C; the 2-D char form can be more memory efficient if you have many strings of similar length. Matlab's string manipulation functions are polymorphic on the two representations, and sometimes exhibit differences like this. A char literal like 'foo' is a degenerate one-string case of the 2-D char form, and Matlab's functions treat it as such.
UPDATE: As of Matlab R2019b or so, Matlab also has a new
string
array type. Double-quoted string literals makestring
arrays instead ofchar
arrays. If you switch to usingstring
arrays, it will solve all your problems here.或者你可以说:
or you can say: