How can I add leading zeros in a column vector?
2 views (last 30 days)
Show older comments
James Condie
on 10 Jun 2016
Commented: Daniel Pare
on 21 Jul 2020
assuming X is my vertical vector of numbers, I've tried
X_with_zeros=sprintf('%04d\n',X)
It prints out what a want but saves it all combined in a 1x1 char array
I tried this loop:
j=length(length(X));
for i=1:j
X_with_zeros=zeros(j,1);
X_with_zeros(i,1)=X(sprintf('%04d',X(i)),1);
end
It's telling me that my assignment doesn't match the dimensions of the subscript, I've tried different ways. I realize I probably need different rules for a list of strings or char array.
2 Comments
Azzi Abdelmalek
on 10 Jun 2016
Your question is confusing, you start with leading zeros and end with date problem. Can you make your question clear. Post an example and explain what you want.
Accepted Answer
Azzi Abdelmalek
on 10 Jun 2016
v=[0 1 2 3 59 100 101]'
w=arrayfun(@(x) sprintf('%04d',x),v,'un',0)
1 Comment
More Answers (1)
See Also
Categories
Find more on Characters and Strings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!