WriteMode append not working when actually appending?

조회 수: 6 (최근 30일)
Adam Johnston
Adam Johnston 2021년 7월 1일
답변: Adam Johnston 2021년 7월 2일
I'm in Matlab R2021a, attempting to output data to an excel spreadsheet. I'm trying the following:
writematrix('outputstring','outputfile.xls','WriteMode','append')
This only works when the file 'outputfile.xls' is empty. If it has anything in it already this error shows up:
Error using writematrix (line 206)
Unable to determine range. Range must be of the form 'A1' (cell), 'A:B' (column-select), '1:5' (row-select),
'A1:B5' (rectangle-select), or a valid named range in the sheet.
I'm trying to output matrices as well as strings but I get this problem whichever data type I use.
Any help appreciated.
Thanks
  댓글 수: 4
Walter Roberson
Walter Roberson 2021년 7월 2일
writematrix cannot handle 3d.
Adam Johnston
Adam Johnston 2021년 7월 2일
I believe it can - In the documentation writematrix, in the Algorithms section right at the bottom it says:
  • writematrix writes out arrays that have more than two dimensions as two dimensional arrays, with the trailing dimensions collapsed.
Anyway if the 3D matrix poses a problem I can work around that - this issue arises when writing any data type to the file.

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

채택된 답변

Adam Johnston
Adam Johnston 2021년 7월 2일
Figured out the issue(s) and thought I'd post here in case anyone else has the same problem.
It appears that the reason the code in the question doesn't work is it uses apostrophes (') instead of inverted commas ("). Switch to inverted commas and matlab will append 1*1 string matrices to its heart's content.
writematrix("outputstring",'outputfile.xls','WriteMode','append')
More generally, it turns out append only works if the matrix to be appended has the same number of columns as the one before it. This can be worked around by padding any matrices that do not meet this condition. For instance, writing a single string followed by an N*N matrix can be done with
outputmatrix=zeros(N)
horpad=repelem(" ",N);
horpad(1)="outputstring"
writematrix(horpad,'outputfile.xls')
writematrix(outputmatrix,'outputfile.xls','WriteMode','append')

추가 답변 (0개)

카테고리

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

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by