How to save vector columnwise in Excel file

Suppose I am generating a column vector randomly 30 times and each time I wish to save it in the Excel column. Can anybody tell me how to do it.
for i=1:30
vec = randn(5,1);
%%Save vec in excel file
end

댓글 수: 2

Jan
Jan 2017년 6월 22일
What is "the Excel column"? Do you want to save all vectors in the same column, or each vector in its own column?
Each vector in different consecutive columns

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

답변 (2개)

Jan
Jan 2017년 6월 22일
편집: Jan 2017년 6월 22일

0 개 추천

ExcelFile = fullfile(tempdir, 'YourFile.xlsx');
for i = 1:30
vec = randn(5,1);
colName = xlsColNum2Str(i);
xlswrite(ExcelFile, vec, sprinft('%s1:%s%d', colName, colName, numel(vec)));
end
It might be easier or faster, to create a matrix and save it as a block:
mat = randn(5, 30);
Range = sprinft('%s1:%s%d', xlsColNum2Str(1), xlsColNum2Str(5), numel(vec));
xlswrite(ExcelFile, mat, Range);
xlswrite can guess the range, if the sheet was specified:
mat = randn(5, 30);
xlswrite(ExcelFile, mat, 1, 'B2');
Nut
Nut 2017년 6월 22일

0 개 추천

Hi,
this code could be valid for you:
for i=1:30
vec = randn(5,1);
% Syntax: xlswrite(filename,A,sheet,xlRange)
xlswrite('Workbook.xlsx',vec,1,[letters num2str(1)])
end
But you need "letters": it is a string corresponding to the column where you want to write the variable "vec".
If to save in Excel for each iteration is not a constraint, you can avoid this. First, you should generate the whole matrix to be exported in Excel, and finally write it into the workbook. I think this way is also more efficient.
mtx = zeros(5,30);
for i=1:30
mtx(:,i) = randn(5,1);
% Syntax: xlswrite(filename,A)
xlswrite('Workbook.xlsx',mtx)
end

댓글 수: 2

I know saving the entire matirx would be easier and faster, but in my case vector are generating dynamically hence I have to save them column by column.
Nut
Nut 2017년 6월 23일
Ok. So, the issue is how to generate the string corresponding to the name of the Excel column. I use a my own code to do this, but you can refer to the answer by Jan Simon: it calls a ready-to-use function from Matlab Exchange, so it should be a better way.

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

카테고리

태그

질문:

2017년 6월 22일

댓글:

Nut
2017년 6월 23일

Community Treasure Hunt

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

Start Hunting!

Translated by