Export data to specified excel rows using writematrix
이전 댓글 표시
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
Walter Roberson
2022년 2월 13일
Perhaps
writematrix(subjects,'Matlab to Excel Subject Data.xlsx', 'Sheet', 2, 'Range', "B"+(i+1) + ":I " + (i+1))
Cassandra Martin
2022년 2월 13일
How about this?
for i = 3:40
writematrix(subjects,'Matlab to Excel Subject Data.xlsx','Sheet',2,'Range',sprintf('B%d:I%d',i+1,i+1))
end
Voss
2022년 2월 13일
But if you want to write to B3:I3 and then continue to B40:I40, those i+1's should be i's because i+1 will give you B4:I4 to B41:I41. So:
for i = 3:40
writematrix(subjects,'Matlab to Excel Subject Data.xlsx','Sheet',2,'Range',sprintf('B%d:I%d',i,i))
end
Cassandra Martin
2022년 2월 13일
Cassandra Martin
2022년 2월 13일
편집: Cassandra Martin
2022년 2월 13일
Voss
2022년 2월 13일
Maybe, as another test, try using writematrix() with a different matrix than the one you want to write (subjects) and see if that works, e.g.:
for i = 3:40
writematrix(randn(1,8),'Matlab to Excel Subject Data.xlsx','Sheet',2,'Range',sprintf('B%d:I%d',i,i))
end
(And be sure you're looking at Sheet 2 when you inspect the xlsx file afterward.)
If that works ok, then can you save your variable subjects to a mat file and attach it here? Or describe it (e.g., what type is it and what size)? Does it have NaNs in it (they will show up as blank cells in the file)?
Also, I notice that you're writing the same thing, subjects, to each row. I don't know if that's actually what you're doing or if that was just the code you posted here as an example. If perhaps subjects is a matrix with 38 rows and 8 columns that you want to write to rows 3 to 40 and columns B to I in the file you can write the whole thing without the for-loop:
writematrix(subjects,'Matlab to Excel Subject Data.xlsx','Sheet',2,'Range','B3:I40')
Cassandra Martin
2022년 2월 13일
편집: Walter Roberson
2022년 2월 14일
Voss
2022년 2월 13일
Since randn(1,8) works, I guess the thing to do would be to make sure the variable subjects has the values you expect right before you write it to file. It looks like maybe the value of subjects is set by the script Master_File, so you might set a breakpoint in there where subjects is set, and/or a breakpoint on the writematrix() line so you can check the value of subjects right before it goes to file.
Cassandra Martin
2022년 2월 14일
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Spreadsheets에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!