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
VBBV
2023년 6월 2일
writematrix (x.','testxxx.xlsx','WriteMode','append')
Use the transpose for x
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Spreadsheets에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!