Export data to specified excel rows using writematrix

조회 수: 5(최근 30일)
Cassandra Martin
Cassandra Martin 2022년 2월 13일
답변: Jeremy Hughes 2022년 2월 14일
Hi,
I am trying to export data to some rows in excel. Here is an example:
writematrix(subjects,'Matlab to Excel Subject Data.xlsx','Sheet',2,'Range','B3:I3')
I have tried using 'WriteMode' and 'append' however, this overrides my headings and titles in the excel file. I would like to fill B3:I3 and continute to B40:I40. I have tried using a for loop which will increase the row by 1.
Here is what I tried:
for i = 3:40
writematrix(subjects,'Matlab to Excel Subject Data.xlsx','Sheet',2,'Range','B',(i+1),':I'(i+1))
end
Unforetunely this does not work. Is there a solution to this?
  댓글 수: 10
Cassandra Martin
Cassandra Martin 2022년 2월 14일
Thank you for all of your help. I've decided it will be best to create one giant matrix and then export it to excel using this method: 'Range','B3:I40'

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

채택된 답변

Jeremy Hughes
Jeremy Hughes 2022년 2월 14일
I believe it should be possible to do this using (almost) the syntax you tried, and there are some suggestions in the comments, but I wanted to share the "WriteMode" parameter as that should do exactly what you're looking for.
for i = 1:5
writematrix(randn(1,3),'test.xlsx','WriteMode','append');
end
A = readmatrix('test.xlsx')
A = 5×3
0.1677 1.1237 1.0494 -1.1193 -0.3099 -0.5449 -1.1076 1.6841 -0.0993 -2.1830 -0.7395 0.7467 -1.0709 0.0737 0.2002
That said, it's going to be a lot slower writing single rows at a time, since writematrix has to open, write, and close the file each time. It's not meant for incremental writing. So the solution to build up the matrix first makes the most sense to me.
The append appoach could be useful if you have very large arrays, but I suspect that the XLSX format will limit you before the array size will.

추가 답변(0개)

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by