xlswrite different columns into excel each run

조회 수: 2 (최근 30일)
smith
smith 2023년 6월 2일
댓글: Rik 2023년 6월 2일
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
Dyuman Joshi
Dyuman Joshi 2023년 6월 2일
Define 'x' as a matrix and save the matrix.
Also, use writematrix instead of xlswrite, as xlswrite is not recommended.
smith
smith 2023년 6월 2일
clc
close all
clear all
for i=1:55
x(i,1) = -1 + (1-(-1))*rand(1,1); %% error band between (-1,1)
end
writematrix (x,'testxxx.xlsx','WriteMode','append')
Now it saves them all under the same column, I'm trying to save each run to the adjacet column, any solution?

댓글을 달려면 로그인하십시오.

답변 (2개)

Rik
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
smith
smith 2023년 6월 2일
I tried that, now it writes them all under one column, I'm trying to append them in different columns, each run in different adjacent column, any possible solution ?
Rik
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
VBBV 2023년 6월 2일
writematrix (x.','testxxx.xlsx','WriteMode','append')

Use the transpose for x

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by