如何格式化字符串以用作 MATLAB 中的结构体字段名称?

发布于 2024-10-03 00:03:24 字数 516 浏览 1 评论 0原文

我想从字符串 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 技术交流群。

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

发布评论

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

评论(4

只是偏爱你 2024-10-10 00:03:24

注意:我猜测您的变量name是一个字符串元胞数组,在这种情况下您将需要使用{}(即< a href="http://www.mathworks.com/help/techdoc/matlab_prog/br04bw6-98.html#br04bw6-108" rel="noreferrer">内容索引)而不是 () (即 单元格索引)从中获取字符串...

与 MATLAB 中的许多问题一样,有多种不同的方法可以解决这个问题...

选项1:您可以使用函数REGEXPREP。以下内容删除连字符、正斜杠和空格:

newName = regexprep(name{i},'[-/\s]','');

这样做的好处是 \s 将匹配并替换所有 空格字符,其中包括普通空格(ASCII 代码 32) )以及制表符、换行符等。

如果您想安全并删除 MATLAB 变量/字段名称,您可以将上面的内容简化为:

newName = regexprep(name{i},'\W','');

选项 2:如果您不需要担心删除除列出的 3 个字符以外的任何内容,您可以使用函数 ISMEMBER 像这样:

newName = name{i};
newName(ismember(newName,'-/ ')) = [];

选项 3:如果您只想保留字母数字字符的所有内容并转储其余部分(连字符、空格、下划线等),您可以使用函数 ISSTRPROP

newName = name{i};
newName = newName(isstrprop(newName,'alphanum'));

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:

newName = regexprep(name{i},'[-/\s]','');

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:

newName = regexprep(name{i},'\W','');

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:

newName = name{i};
newName(ismember(newName,'-/ ')) = [];

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:

newName = name{i};
newName = newName(isstrprop(newName,'alphanum'));
廻憶裏菂餘溫 2024-10-10 00:03:24

最简单的方法是使用内置函数 genvarname。它会让名字看起来更难看,但它保证是一个有效的名字,并且会保留你原来的独特性。

如果您只想删除特定字符,可以使用 regexprep:

regexprep('foo- /foo- /foo', '[- \/]', '')

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:

regexprep('foo- /foo- /foo', '[- \/]', '')
提赋 2024-10-10 00:03:24

字符串只是数组,因此您可以执行以下操作:

name(name == '-' | name == '/' | name = ' ') = [];

就您的总体目标而言,还有更多字符在结构名称中无效。您必须定义一组允许的字符,并消除该组中不存在的所有内容。

例如:

function i = isAllowed(str)

i = (str >= '0' & str <= '9') ...
  | (str >= 'a' & str <= 'z') ...
  | (str >= 'A' & str <= 'Z');


...

name(~isAllowed(name)) = [];

Strings are just arrays, so you could do something like:

name(name == '-' | name == '/' | name = ' ') = [];

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.:

function i = isAllowed(str)

i = (str >= '0' & str <= '9') ...
  | (str >= 'a' & str <= 'z') ...
  | (str >= 'A' & str <= 'Z');


...

name(~isAllowed(name)) = [];
我的鱼塘能养鲲 2024-10-10 00:03:24

这是另一个解决方案:

name = 'some/path/file-name ext';    %# sample string
blacklist = {'-' '/' ' '};           %# list of character not allowed

idx = cell2mat( cellfun(@(c)strfind(name,c), blacklist, 'UniformOutput',false) );
name(idx) = '_';                    %# you can remove/replace those locations

>> name
 name =
 some_path_file_name_ext

Here's another solution:

name = 'some/path/file-name ext';    %# sample string
blacklist = {'-' '/' ' '};           %# list of character not allowed

idx = cell2mat( cellfun(@(c)strfind(name,c), blacklist, 'UniformOutput',false) );
name(idx) = '_';                    %# you can remove/replace those locations

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