Replacing One Character Issue

10 Ansichten (letzte 30 Tage)
Kimi Raikonnen
Kimi Raikonnen am 3 Mär. 2023
Kommentiert: Voss am 3 Mär. 2023
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 ?

Antworten (1)

Voss
Voss am 3 Mär. 2023
Bearbeitet: Voss am 3 Mär. 2023
"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 Kommentare
Kimi Raikonnen
Kimi Raikonnen am 3 Mär. 2023
Actually, disp is just an alternative to fprint isn't it ?
Voss
Voss am 3 Mär. 2023
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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Cell Arrays finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by