-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxlsColNum2Str.m
More file actions
54 lines (49 loc) · 1.98 KB
/
xlsColNum2Str.m
File metadata and controls
54 lines (49 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
function [colChar]=xlsColNum2Str(colNum)
%XLSCOLNUM2STR takes in an array of numbers and returns a cellular array
%of the same size with cell of corresponding Excel column names.
%
%For example:
%n=[1 10;
% 53 256]
%c=xlsColNum2Str(n);
%c={'A' , 'J';
% 'BA', 'IV'}
%Note: up to Excel 2003 the number of columns was limited to 256, as of
%Excel 2007 the number of columns has increased to 16,384 or 'XFD'
%This function is designed to take accept any integer so proper handling
%of the number of columns should be taken care of outside this function
colChar=cell(size(colNum)); %blank cell array
% find max number of characters (AA n=2)
numOfChars=ceil(max(colNum)/26)-1;
n=1;
while numOfChars>=1
numOfChars=ceil(numOfChars/26)-1;
n=n+1;
end
remainder=num2cell(colNum);
for s=n:-1:1
if s>1
%find limits
% if n=2 then the columns go from AA to ZZ or 27 to 702
L=sum(26.^(1:s-1))+1; % lower limit
U=sum(26.^(1:s)); %upper limit
%place current character to right of previous
colChar(colNum>=L & colNum<=U) = ...
cellfun(@(x,y) ([x char(ceil((y-(L-1))/26^(s-1))+64)]),...
colChar(colNum>=L & colNum<=U),... % x
remainder(colNum>=L & colNum<=U),... % y
'UniformOutput',false);
%calculate the remaining string
%for example if last string was 'ABA' the 'A' was placed to the
%right of the previous string and now 'BA' is remaining
remainder(colNum>=L & colNum<=U)=...
cellfun(@(x,y) (y-26^(s-1)*(double(x(end))-64)),...
colChar(colNum>=L & colNum<=U),...
remainder(colNum>=L & colNum<=U),'UniformOutput',false);
colNum=cell2mat(remainder);
else
colChar=cellfun(@(x,y) ([x char(y+64)]),...
colChar,remainder,'UniformOutput',false);
end
end
end