如何格式化字符串以用作 MATLAB 中的结构体字段名称?
我想从字符串 name(i)< 中删除连字符 (
-
)、斜线 (/
) 和空格 () /code> 以便我可以将其用作结构字段名称。
这是我目前使用函数 strrep
:
cell2mat(strrep(strrep(strrep(name(i), '-',''),'/',''),' ', ''))
我还尝试了其他变体,例如:
strrep(name(i),{'-','/'},{'',''});
strrep(name(i),['-','/'],['','']);
什么是更有效的方法?
I want to remove hyphens (-
), slashes (/
) and white space () from a string
name(i)
so that I can use it as a structure field name.
This is the ugly way I am currently doing it using the function strrep
:
cell2mat(strrep(strrep(strrep(name(i), '-',''),'/',''),' ', ''))
I have also tried other variations, such as:
strrep(name(i),{'-','/'},{'',''});
strrep(name(i),['-','/'],['','']);
What is a more efficient way of doing this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
注意:我猜测您的变量
name
是一个字符串元胞数组,在这种情况下您将需要使用{}
(即< a href="http://www.mathworks.com/help/techdoc/matlab_prog/br04bw6-98.html#br04bw6-108" rel="noreferrer">内容索引)而不是()
(即 单元格索引)从中获取字符串...与 MATLAB 中的许多问题一样,有多种不同的方法可以解决这个问题...
选项1:您可以使用函数REGEXPREP。以下内容删除连字符、正斜杠和空格:
这样做的好处是
\s
将匹配并替换所有 空格字符,其中包括普通空格(ASCII 代码 32) )以及制表符、换行符等。如果您想安全并删除 MATLAB 变量/字段名称,您可以将上面的内容简化为:
选项 2:如果您不需要担心删除除列出的 3 个字符以外的任何内容,您可以使用函数 ISMEMBER 像这样:
选项 3:如果您只想保留字母数字字符的所有内容并转储其余部分(连字符、空格、下划线等),您可以使用函数 ISSTRPROP:
Note: I'm guessing your variable
name
is a cell array of strings, in which case you will want to use{}
(i.e. content indexing) instead of()
(i.e. cell indexing) to get the strings from it...As with many problems in MATLAB, there are a number of different ways you can solve this...
Option 1: You could use the function REGEXPREP. The following removes hyphens, forward slashes, and whitespace:
The benefit here is that the
\s
will match and replace all whitespace characters, which includes a normal space (ASCII code 32) as well as tabs, newlines, etc..If you want to be safe and remove every character that is not valid in a MATLAB variable/field name, you can simplify the above to this:
Option 2: If you don't need to worry about removing anything other than the 3 characters you listed, you could use the function ISMEMBER like so:
Option 3: If you want to just keep everything that is an alphanumeric character and dump the rest (hyphens, whitespace, underscores, etc.), you could use the function ISSTRPROP:
最简单的方法是使用内置函数 genvarname。它会让名字看起来更难看,但它保证是一个有效的名字,并且会保留你原来的独特性。
如果您只想删除特定字符,可以使用 regexprep:
The easiest way is to use the built in function genvarname. It will make the name look uglier, but its guaranteed to be a valid name AND it will retain you original uniqueness.
If you just want to remove specific characters, you can use regexprep:
字符串只是数组,因此您可以执行以下操作:
就您的总体目标而言,还有更多字符在结构名称中无效。您必须定义一组允许的字符,并消除该组中不存在的所有内容。
例如:
Strings are just arrays, so you could do something like:
With respect to your overall goal, there are many more characters that aren't valid in a struct name. You're bet off defining a set of allowable characters, and eliminating everything that isn't in that set.
e.g.:
这是另一个解决方案:
Here's another solution: