xlswrite different columns into excel each run
2 次查看(过去 30 天)
显示 更早的评论
Hello, I have this issue where, i have a matrix x that i want to write on an excel column every run, but i do not want it to overwrite itself and i do not want to specify the range every run (A1:A66/B1:B66), is there a way to make this work?
clc
close all
clear all
for i=1:55
x(i,1) = -1 + (1-(-1))*rand(1,1);
end
xlswrite ('test.xlsx', x)
Thank you!
2 个评论
回答(2 个)
Rik
2023-6-2
If you switch to writematrix, you can set 'WriteMode' to 'append'.
If you want to stick with xlswrite, you will have to read the original contents of the file and generate the appropriate range for the data you want to write.
2 个评论
Rik
2023-6-2
Then you will have to use my second suggestion and generate the range based on the current contents of the file, and on what you want to write. You can use the function below.
function ExcelColName=get_ExcelColName(col_val)
%convert a col value to the Excel col name (so 27 to AA, 705 to AAC)
validateattributes(col_val,{'numeric'},{'scalar','nonzero','integer'})
base=26;
col_val=col_val-1;
digs=floor(log(col_val)/log(base))+1;%number of 'digits' (i.e. letter positions)
ExcelColName=zeros(1,digs);
for cur_d=digs:-1:1
ExcelColName(cur_d)=mod(col_val,base);
col_val=(col_val-ExcelColName(cur_d))/base;
end
if isempty(ExcelColName),ExcelColName=0;end%in case of col_val input is 1
ExcelColName(end)=ExcelColName(end)+1;
ExcelColName=char(ExcelColName+64);
end
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Spreadsheets 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!