time | Calls | line |
---|
| | 1 | function s = columnLetter(d)
|
| | 2 | % COLUMNLETTER(D) returns the representation of D as a spreadsheet column letter,
|
| | 3 | % expressed as 'A'..'Z', 'AA','AB'...'AZ', and so on.
|
| | 4 | %
|
| | 5 | % Examples:
|
| | 6 | % dec2base(1) returns 'A'
|
| | 7 | % dec2base(26) returns 'Z'
|
| | 8 | % dec2base(27) returns 'AA'
|
| | 9 | %
|
| | 10 | % See also matlab.io.spreadsheet.internal.columnNumber
|
| | 11 |
|
| | 12 | % Copyright 2014-2016 The MathWorks, Inc.
|
| | 13 |
|
< 0.001 | 5 | 14 | persistent powersOf26
|
< 0.001 | 5 | 15 | if isempty(powersOf26)
|
| | 16 | powersOf26 = 26.^(1:11);
|
< 0.001 | 5 | 17 | end
|
| | 18 |
|
< 0.001 | 5 | 19 | digits = 1;
|
< 0.001 | 5 | 20 | begin = 0;
|
< 0.001 | 5 | 21 | current_sum = 26;
|
| | 22 | % This calculates the number of "letter-digits" in the output
|
< 0.001 | 5 | 23 | while d > current_sum
|
| | 24 | digits = digits + 1;
|
| | 25 | begin = current_sum;
|
| | 26 | current_sum = begin + powersOf26(digits);
|
| | 27 | end
|
| | 28 |
|
< 0.001 | 5 | 29 | idx = zeros(1,digits);
|
< 0.001 | 5 | 30 | pos = d - begin;
|
| | 31 |
|
| | 32 | % Find the leftmost "letter-digits"
|
< 0.001 | 5 | 33 | for i = digits:-1:3
|
| | 34 | remainder = rem(pos-1, powersOf26(i-1)) + 1;
|
| | 35 | idx(i) = (pos - remainder)/powersOf26(i-1) + 1;
|
| | 36 | pos = remainder;
|
| | 37 | end
|
| | 38 |
|
| | 39 | % Find the right most "letter-digits"
|
< 0.001 | 5 | 40 | if digits >= 2
|
| | 41 | idx(1) = rem(pos-1, 26) + 1;
|
| | 42 | idx(2) = (pos - idx(1))/26 + 1;
|
< 0.001 | 5 | 43 | else
|
< 0.001 | 5 | 44 | idx(1) = pos;
|
< 0.001 | 5 | 45 | end
|
| | 46 |
|
| | 47 | % Write out the reverse digits using char
|
< 0.001 | 5 | 48 | s = char('A'-1+idx(end:-1:1));
|
< 0.001 | 5 | 49 | end
|
Other subfunctions in this file are not included in this listing.