从元胞数组中查找字符串并创建新的元胞数组
Find strings from an cell array and create a new cell array
我想使用 matlab 从元胞数组 (m x n)
中找到字符串,并将这些识别出的字符串添加到新的元胞数组 (m x n)
中,例如:
Human(i,1)={0
1
34
eyes_two
55
33
ears_two
nose_one
mouth_one
631
49
Tounge_one}
我想删除数字而只有字符串
New_Human(i,1)={eyes_two
ears_two
nose_one
mouth_one
tounge_one}
根据您的评论,听起来您的所有数据都存储为字符串。在这种情况下,您可以使用以下方法删除所有代表有效数字的字符串。
H = {'0'; '1'; '34'; 'eyes_two'; '55'; '33'; 'ears_two'; 'nose_one'; 'mouth_one'; '631'; '49'; 'Tounge_one'};
idx = cellfun(@(x)isnan(str2double(x)), H);
Hstr = H(idx)
输出
Hstr =
'eyes_two'
'ears_two'
'nose_one'
'mouth_one'
'Tounge_one'
代码确定哪些字符串不代表有效数值。这是通过检查 str2double
函数 returns 是否在每个字符串上产生 NaN
来实现的。如果您想了解更多有关其工作原理的信息,我建议您阅读有关 cellfun
.
的文档
我想使用 matlab 从元胞数组 (m x n)
中找到字符串,并将这些识别出的字符串添加到新的元胞数组 (m x n)
中,例如:
Human(i,1)={0
1
34
eyes_two
55
33
ears_two
nose_one
mouth_one
631
49
Tounge_one}
我想删除数字而只有字符串
New_Human(i,1)={eyes_two
ears_two
nose_one
mouth_one
tounge_one}
根据您的评论,听起来您的所有数据都存储为字符串。在这种情况下,您可以使用以下方法删除所有代表有效数字的字符串。
H = {'0'; '1'; '34'; 'eyes_two'; '55'; '33'; 'ears_two'; 'nose_one'; 'mouth_one'; '631'; '49'; 'Tounge_one'};
idx = cellfun(@(x)isnan(str2double(x)), H);
Hstr = H(idx)
输出
Hstr =
'eyes_two'
'ears_two'
'nose_one'
'mouth_one'
'Tounge_one'
代码确定哪些字符串不代表有效数值。这是通过检查 str2double
函数 returns 是否在每个字符串上产生 NaN
来实现的。如果您想了解更多有关其工作原理的信息,我建议您阅读有关 cellfun
.