Matlab中将数字转excel列字母

1.问题背景

在matlab中将表格数据写入excel时,有时要求从第4列开始写入,需要将“4”转化为“D”。(举例)

因为,第4列开始写入时,使用writematrix(mydata, filename,Sheet=1,'Range','D1'),其中D只能写字母,不能写数字,带来麻烦。于是本博客目的是用matlab将数字转化为excel列字母。

2.法一:matlab将数字转化为excel列字母

构造函数numberToExcelColumn将数字转列字母,使用时可直接调用。

columnLetter = numberToExcelColumn(100);
disp(columnLetter); % 第100列输出结果为 'CV'

function columnLetter = numberToExcelColumn(number)
%目的:数字转化为excel的列字母
    if number < 1 || number ~= fix(number)
        error('Input must be a positive integer.');
    end
    
    alphabet = 'A':'Z';
    n = length(alphabet);
    columnLetter = '';
    
    while number > 0
        remainder = mod(number - 1, n) + 1;
        columnLetter = [alphabet(remainder), columnLetter];
        number = (number - remainder) / n;
    end
end

 3.法二:绕过此问题

可以考虑将输出至第n列转化为输出至第n行,这样可以绕过列字母问题,将D1转化为A4,E1转化为A5。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值