0

if I have char array,ex: A='hello' of size 5 chars and I want to pad it with another character or set of characters to make its size =16 how can I pad this array in matlab ? and what should I use for padding can I use zero or I must use another character ?

2
  • Have you tried pad? Commented Nov 4, 2017 at 4:33
  • I'm using matlab R2014a ,it doesn't support creating strings Commented Nov 4, 2017 at 4:45

1 Answer 1

1

You can use sprintf:

result = sprintf('%-16s','hello');

Or it can be created using array concatenation:

ex = 'hello';
result = [ex blanks(16-length(ex))];

Padding with other characters(e.g. 'a'):

ex = 'hello';
result = [ex repmat('a',1,16-length(ex))];

*As of MATLAB R2016b you can use pad function.

Sign up to request clarification or add additional context in comments.

1 Comment

thank you , can I pad the array with other characters (e.g 'a') ? and how ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.