How to iterate through the alphabet like Excel does
Show older comments
Hi, I am really frustrated about that issue, because it seems to me, that it is really easy, but I dont get it running...
So I try to write some cells in an Excel sheet, in a specific range. The range is supplied to the xlswrite or writetable function in the Excel style, i.e. A1:AB15. I would like to create a string that specifies the range dynamically. So I need a function, that converts numbering (1,2,3 ...) to "excel alphabetical numbering" (A,B,C, ..., Z, AA, AB...). I tried a lot around with some base 26 and base 27 stuff. But it still doesn't give me satisfying results. In the end, I don't really care anymore about the Excel table numbering stuff, i just want to get this function alphabetStr = num2ExcelAlpha(num) to produce the correct string.
My initial thoughts were: The alphabet is some kind of base 26 enumeration (sorry for the wrong terms but I am an engineering grad, not a maths grad...). So basically I can get the number of digits of my "alphabetical number" with some log to the base 26 stuff. It didn't work for me.
In the end I am counting the digits kind of "manually". I bet there is something easier with logarithms, but I didn't figure it out.
digits = 0;
while num > 0
num = num - 26^(digits+1);
digits = digits+1;
end
Now, that we have the number of digits, I would like to assign every number a string consisting of digits characters:
alph = 'A':'Z'
str = [];
for i = 1:digits
idx = floor(num/26^(digits-i));
if idx == 0
idx = 26;
end
str = [str alph(idx)];
num = num - floor(num/26^(digits-i)); %probalby *26^(digits-i); probably ceil(); something is wrong here
if num == 0
num = 26^(digits-i-1);
end
end
You see, this is already really confusing what I did there, but this is already my 50th attempt, and it still doesn't give me the right characters consistantly. Mostly, whether the strings with 'A' or 'Z' are missing, wrong, or the index is 0 and it throws an error.
- Is there an easier solution to this?
- If not included in 1: What did I miss regarding the maths part of this problem?
Thanks a lot for your help!
4 Comments
Fangjun Jiang
on 19 Jan 2021
what version of MATLAB?
Alexander Keller
on 19 Jan 2021
"What did I miss regarding the maths part of this problem?"
Excel column indexing is not really base 26 nor base 27, because the zero digit is completely missing.
A few years ago I reviewed all of the threads on this topic, and found that many answers were not reliable above one or two characters. Many solutions use kludges that limit the number of columns and give no warning if they return wrong values.... ugh! Actually the required general solution WHILE-loop is really not that complex, I showed one version in an answer here:
You can find some FEX approaches here, most of them are also buggy:
It is possible to do this (correctly!) without any loop by figuring out the repdigits in base 26... it only takes about 3-4 lines of code.
EDIT: For recent MATLAB versions (which?), probably the best method was given by Fangjun Jiang here:
import matlab.io.spreadsheet.internal.columnLetter
columnLetter(16384)
dpb
on 20 Jan 2021
How does one know of such other than by such random postings???
Accepted Answer
More Answers (1)
Fangjun Jiang
on 19 Jan 2021
0 votes
- In old versions of MATLAB, you can find that function inside xlswrite.m. I used to copy that for my own use.
- In R2019b, it seems it is using "import matlab.io.spreadsheet.internal.columnLetter". You could do the same.
- dec2base() is the related function. You want base to be 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' which is 26. But when you specify the base to be 26, the function output string of '0123456789ABCDEFGHIJKLMNOP'. You could do some further conversion.
2 Comments
Fangjun Jiang
on 19 Jan 2021
Stephen23
on 19 Jan 2021
The missing zeros make it more complex than just converting the base.
Categories
Find more on Logical 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!