Turning numbers into letters based on alphabetical order

Hi all, I am trying to export multiple matrix variable that change in size to an excel file. To get their spacing right I need to give a cell name (eg. 'B3') where I want the variable to be placed. However, I want to be able to space my matrices based on their number of columns, which mean taking a length() command and turning it into a letter. Is there any built in way in matlab to do this. an example of what I'm looking for is below.
function(7)
ans G (because it is the 7th letter in the alphabet)
There may be an easier way of doing this with xlswrite but I dont know it. Thanks
Brendan

1 Comment

num=26;
position = alphabet(num)
function position=alphabet(num)
mult=floor(num/26);
diff=num-26*mult;
symbols = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
range=26.*[1:26];
if num>26
for i=1:length(range)
if (num >= range(i) && num <= range(i+1))
start=string(symbols(i))
end
end
if diff > 0
extension=string(symbols(diff))
position=char(start+extension+'1')
else diff=0
extension=string(symbols(26))
position=char(start+extension+'1')
end
else
position=string(symbols(num));
position=char(position + '1');
end
end

Sign in to comment.

 Accepted Answer

Hi Brendan,
something like this:
n = 7;
char('A'+(n-1))
What I forgot: you might also think about taking
n1 = rem(n, 26);
n2 = floor(n/26);
in order to have e.g. "AE" for n=31 ...
Titus

4 Comments

Thanks this worked great.
This problem resembles converting to base 26, except that we want a different set of symbols than dec2base gives, and the handling of zeros is a bit different. Borrowing from the dec2base code, this function converts a number to an excel-style column index for arbitrary n:
function lets = letters(nums)
lets = arrayfun(@(n)num2char(n),nums,'UniformOutput',0);
function s = num2char(d)
b = 26;
n = max(1,round(log2(d+1)/log2(b)));
while (b^n <= d)
n = n + 1;
end
s(n) = rem(d,b);
while n > 1
n = n - 1;
d = floor(d/b);
s(n) = rem(d,b);
end
n = length(s);
while (n > 1)
if (s(n) <= 0)
s(n) = b + s(n);
s(n-1) = s(n-1) - 1;
end
n = n - 1;
end
s(s<=0) = [];
symbols = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
s = reshape(symbols(s),size(s));
end
end
For example:
letters([1,26,27,52,53,26*26,26*26+1,27*26,27*26+1,34*26+1])
gives:
'A' 'Z' 'AA' 'AZ' 'BA' 'YZ' 'ZA' 'ZZ' 'AAA' 'AHA'
Julian, your answer is probably the most comprehensive so you may consider reposting it as an actual answer rather than a comment, so you can at least get some votes for it, even if it can't be the accepted answer anyway.
That is, if you want some reputation points.
Or you might submit this to the File Exchange, if nothing similar is there already ...

Sign in to comment.

More Answers (1)

There's not an explicit builtin for this, but it is easy to write:
function c = letterRankToLetter(n)
c = char('A'+n-1);
end
This will only work for n in the range 1-26, so you might want to put some error-checking in there.

Tags

Community Treasure Hunt

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

Start Hunting!