How to pad zero in string using sprintf
Mostrar comentarios más antiguos
Hello, I have some string
'1010'
'111'
'010'
'10'
'10111'
How to pad zero after the string by using sprint function to be like this
'10100'
'11100'
'01000'
'10000'
'10111'
Respuestas (3)
This should work on both char arrays as well as strings:
a = { '1010'
'111'
'010'
'10'
'10111'}
b = pad(a, '0')
3 comentarios
the cyclist
el 5 de En. de 2023
Ah, didn't know that!
Stephen23
el 5 de En. de 2023
Fangjun Jiang
el 5 de En. de 2023
good to know
a={'1010'
'111'
'010'
'10'
'10111'};
b=char(a)
b(b==' ')='0'
@Fangjun Jiang gave a great answer for character arrays (which is what you show that you have).
This is a case where MATLAB's new-ish (R2016) string data type might suit your need better, if you can go upstream in your code to use that type.
string = ["1010";
"111";
"010";
"10";
"10111"];
pad(string,"0")
1 comentario
Stephen23
el 5 de En. de 2023
+1 nice
Categorías
Más información sobre String Parsing en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!