Replacing One Character Issue

조회 수: 10 (최근 30일)
Kimi Raikonnen
Kimi Raikonnen 2023년 3월 3일
댓글: Voss 2023년 3월 3일
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개)

Voss
Voss 2023년 3월 3일
편집: Voss 2023년 3월 3일
"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)
c% % %
picture(1) = '$';
disp(picture)
$% % %
  댓글 수: 5
Kimi Raikonnen
Kimi Raikonnen 2023년 3월 3일
Actually, disp is just an alternative to fprint isn't it ?
Voss
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Cell Arrays에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by