Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Right Pad String with spaces Matlab

Tags:

padding

matlab

So the following code can left pad a string with spaces keeping a max length of max_len (spaces and string included).

max_len = 20; 

spacing_arg = ['% ', max_len,'s'];

padded_string = sprintf(spacing_arg, str);

Now how to do it efficiently with right padding?

like image 362
BAR Avatar asked Dec 06 '25 09:12

BAR


1 Answers

Is this what you mean?

max_len = 20; 
spacing_arg = ['%-', num2str(max_len),'s'];
padded_string = sprintf(spacing_arg, str);
like image 129
German Gomez-Herrero Avatar answered Dec 09 '25 00:12

German Gomez-Herrero



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!