How do I increment a 4-digit number in a character array?
Show older comments
I've got a 1x4 character array containing a 4-digit number.
IRN = '0050';
I'm looking for a way to increment this number in a for loop. For example, with 3 iterations the output of the for loop would be as follows;
MRN =
0051
0052
0053
Can MATLAB increment numbers, within a character array, in a for loop?
Accepted Answer
More Answers (1)
Walter Roberson
on 23 Feb 2017
IRN = '0098';
L = length(IRN);
for k = 1 : 5
for J = L : -1 : 1;
if IRN(J) == '9'
IRN(J) = '0';
else
IRN(J) = IRN(J)+1;
break;
end
end
disp(IRN)
end
Categories
Find more on Data Type Conversion in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!