Matlab 替换包含';#的每个字符串';
我有一个包含字符串的单元格数组,如下面的dataT1。如何将包含“#”的所有字符串替换为字母“O”,而不替换其他内容(字符串中没有数字)Matlab 替换包含';#的每个字符串';,matlab,Matlab,我有一个包含字符串的单元格数组,如下面的dataT1。如何将包含“#”的所有字符串替换为字母“O”,而不替换其他内容(字符串中没有数字) 您可以使用一个简单的循环 如果要将#替换为O,请使用以下方法: for c = 1:numel(data) % check for character array type in case cell also has numeric values if ischar(data{c}) % replace hashes with
您可以使用一个简单的循环 如果要将
#
替换为O
,请使用以下方法:
for c = 1:numel(data)
% check for character array type in case cell also has numeric values
if ischar(data{c})
% replace hashes with 'O'
data{c} = strrep(data{c}, '#', 'O');
end
end
如果要将包含#
的整个字符串替换为仅包含'O'
的字符串,请使用以下命令:
for c = 1:numel(data)
% check for character array type in case cell also has numeric values
if ischar(data{c})
% Search for # within string
if strfind( data{c}, '#' ) > 0
% replace string with 'O'
data{c} = 'O';
end
end
end
for c = 1:numel(data)
% check for character array type in case cell also has numeric values
if ischar(data{c})
% Search for # within string
if strfind( data{c}, '#' ) > 0
% replace string with 'O'
data{c} = 'O';
end
end
end