Replacing One Character Issue
显示 更早的评论
picture = '% % %'
picture = strrep(picture,picture(1),'$')
I want to replace the 1st character with a $, but it replaces all the characters with a $. I think this is because all the characters are identical. How can I fix this without changing much of the program ?
回答(1 个)
"I want to replace the 1st character with a $"
picture = '% % %';
disp(picture)
picture(1) = '$';
disp(picture)
Note that that replaces the first character of picture with "$" regardless of what that character was (i.e., it does not replace the first character that's "%").
picture = 'c% % %';
disp(picture)
picture(1) = '$';
disp(picture)
5 个评论
If you actually want to replace the first character that's "%" with "$", then:
picture = '% % %';
disp(picture)
idx = find(picture == '%', 1);
if ~isempty(idx)
picture(idx) = '$';
end
disp(picture)
picture = 'c% % %';
disp(picture)
idx = find(picture == '%', 1);
if ~isempty(idx)
picture(idx) = '$';
end
disp(picture)
Kimi Raikonnen
2023-3-3
Kimi Raikonnen
2023-3-3
Kimi Raikonnen
2023-3-3
Voss
2023-3-3
The difference is that one replaces the first character and one replaces the first '%'.
disp does no conversion.
disp displays to the command line, yes.
类别
在 帮助中心 和 File Exchange 中查找有关 Workspace Variables and MAT Files 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!