Matlab strcat 函数出现空格问题

发布于 2024-08-04 13:44:27 字数 149 浏览 7 评论 0原文

我正在尝试实现此目标:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

远昼 2024-08-11 13:44:27

尽管 STRCAT 忽略尾随空格,它仍然保留前导空格。试试这个:

strcat('red',' yellow',' white')

或者,您可以只使用串联语法:

['red ' 'yellow ' 'white ']

Although STRCAT ignores trailing white space, it still preserves leading white space. Try this:

strcat('red',' yellow',' white')

Alternatively, you can just use the concatenation syntax:

['red ' 'yellow ' 'white ']
话少情深 2024-08-11 13:44:27

来自 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. "

不回头走下去 2024-08-11 13:44:27

事实上,你可以简单地使用空格的ASCII码:32。因此,你可以这样解决问题:

str = strcat('red', 32, 'yellow', 32, 'white');

然后你会得到str = 'red Yellow White'

In fact, you can simply use the ASCII code of space: 32. So, you can solve the problem like this:

str = strcat('red', 32, 'yellow', 32, 'white');

Then you will get str = 'red yellow white'.

流绪微梦 2024-08-11 13:44:27

您可以通过将 strcat() 或类似函数放入单元格中来保护尾随空格。

str = strcat({'red '}, {'yellow '}, {'white '})
str = str{1}

在这个基本示例中不是很有用。但如果您最终对字符串进行“矢量化”操作,那么它会很方便。常规数组串联不会执行 strcat 所做的一对多串联。

strs = strcat( {'my '}, {'red ','yellow ','white '}, 'shirt' )

将“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.

str = strcat({'red '}, {'yellow '}, {'white '})
str = str{1}

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.

strs = strcat( {'my '}, {'red ','yellow ','white '}, 'shirt' )

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 make string arrays instead of char arrays. If you switch to using string arrays, it will solve all your problems here.

橪书 2024-08-11 13:44:27

或者你可以说:

str = sprintf('%s%s%s', 'red ', 'yellow ', 'white ')

or you can say:

str = sprintf('%s%s%s', 'red ', 'yellow ', 'white ')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文