Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

# How to write several results in the same sheet of a Xls file ?

조회 수: 2 (최근 30일)
Niki 2011년 8월 30일
마감: MATLAB Answer Bot 2021년 8월 20일
As a matter of fact I would like to obtain some results which are related to each other . I would like to compare them to each other but in the same sheet. It can be easily write the "data" from workspace to a xls file as follow
>> xlswrite('C:\path\filename.xls',data, 'Name of sheet');
right now my question is that, if I want to put new result in the same sheet but I do not want to lose the first result also, what should I do?
the dimension of the first result is not clear (for example it can be a matrix 4*4)
and the second result also, (it can be a matrix 6*6)

### 답변 (4개)

Rick Rosson 2011년 8월 30일
In the following code, I am assuming that your data is stored in arrays A, B, C, and D of various sizes:
filename = 'C:\mypath\myfile.xls';
sheet = 'mySheet';
row = 1;
xlswrite(filename,A,sheet,['A' num2str(row) ] );
row = row + size(A,1) + 3;
xlswrite(filename,B,sheet,['A' num2str(row) ] );
row = row + size(B,1) + 3;
xlswrite(filename,C,sheet,['A' num2str(row) ] );
row = row + size(C,1) + 3;
xlswrite(filename,D,sheet,['A' num2str(row) ] );
row = row + size(D,1) + 3;
HTH.
Rick
##### 댓글 수: 4이전 댓글 2개 표시이전 댓글 2개 숨기기
Andrei Bobrov 2011년 8월 30일
Hi, Friends! Change 'row' on 'Row' in 'num2str(row)'.
Fangjun Jiang 2011년 8월 30일
Good catch, Andrei! I've been used to mixed case but I copied Rick's code.

Fangjun Jiang 2011년 8월 30일
If it's easy to combine all the data into one, then I recommend combining them first and then write.
data1=rand(4,4);
data2=magic(4);
xlswrite('test.xls',[data1;data2],'SheetName');
Or you can write to xls file multiple times, use Range to specify the position on the spreadsheet.
xlswrite(File,Data,Sheet, Range)
You can use size(data1) to get the size of your data.
##### 댓글 수: 2없음 표시없음 숨기기
Niki 2011년 8월 30일
as you know, your code works well, but there is some problem with that, because exactly the second result with copied after the first time,
on the other hand, I do not have only two results,
maybe 100 results,
from another point of view I would like to mention all the information of each cell therefore I can not use of yours because i do not have space for the other information
Fangjun Jiang 2011년 8월 30일
I don't fully understand your comment. But anyway, Rick's solution should work out for you if you have hundreds of matrices to write.

Chirag Gupta 2011년 8월 30일
You can do this accurately, if you know the dimensions of the results.
Otherwise, if you have good idea for maximum size of the result you should still be able to do this fairly accurately:
xlswrite('filename',data,'MyResultsSheet','A1'); % Write this result to the first cell of the sheet
% Assuming that the data max size is going to be 10 x 10
xlswrite('filename',data,'MyResultsSheet','K1');
##### 댓글 수: 0이전 댓글 -2개 표시이전 댓글 -2개 숨기기

Andrei Bobrov 2011년 8월 30일
A=randi(4,3),B=randi(5,4,6),C=randi(7,3,8),D=randi(70,4,3)
Am = {A B C D}
N = cumsum([1 cellfun('size',Am(1:end-1),1)+1])
for i1 = 1:numel(Am)
xlswrite('C:\path\filename.xls',Am{i1}, 'Name of sheet',['A',num2str(N(i1))]);
end
##### 댓글 수: 1이전 댓글 -1개 표시이전 댓글 -1개 숨기기
Niki 2011년 8월 30일
sounds good Andrei, but there is two problems,
first of all, we do not have all the results together, so that in each for example hour one result will be saved in workspace
the second problem is that there is only one line space between each pair of result

이 질문은 마감되었습니다.

### Community Treasure Hunt

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

Start Hunting!

Translated by