如何 运行 xlswrite 循环函数中的公式

How to run xlswrite with formula in loop function

我有一个大单元格 (2500x3) 数组,我想将其导出为 .xls 文件,但我想包含一个可用于 [=22] 的公式 ('=SUM(B1:C1)') =].

下面的代码工作正常,但我希望 B1C1 在每次迭代中增加 1'=SUM(B1:C1)', '=SUM(B2:C2)', '=SUM(B3:C3)' 等)。我怎样才能做到这一点?我现在的循环代码是:

for II = 1:length(out)
  out{II,4}='=SUM(B1:C1)';
end

xlswrite('Book1.xls',out)

您可以使用 sprintf:

for II = 1:3
  out{II} = sprintf('=SUM(B%u:C%u)', II, II);
end

这给了我们:

out =

  1×3 cell array

    {'=SUM(B1:C1)'}    {'=SUM(B2:C2)'}    {'=SUM(B3:C3)'}