editing in string array

10 views (last 30 days)
ANKUR KUMAR
ANKUR KUMAR on 1 Nov 2017
Edited: KSSV on 1 Nov 2017
a={'10','7','5','9','11','12','12','4'}
I want to put 0 at the starting of all those whose length is 1. How can I do this easily?

Accepted Answer

KSSV
KSSV on 1 Nov 2017
Edited: KSSV on 1 Nov 2017
a={'10','7','5','9','11','12','12','4'} ;
L = cellfun(@length,a) ;
idx = L==1 ;
a(idx) = strcat('0',a(idx)) ;
  3 Comments
KSSV
KSSV on 1 Nov 2017
cellfun works on the cells.....it is nothing but a for loop on each cell. arrayfun works on rows/ cols of matrices.
Stephen23
Stephen23 on 1 Nov 2017
Edited: Stephen23 on 1 Nov 2017
More accurately:
  • cellfun applies the function to the contents of each cell of a cell array.
  • arrayfun applies the function to each element of any array (which could be numeric, cell, etc).
Use whichever suits your purpose.

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!