WriteMode append not working when actually appending?

7 vues (au cours des 30 derniers jours)
Adam Johnston
Adam Johnston le 1 Juil 2021
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 commentaires
Walter Roberson
Walter Roberson le 2 Juil 2021
writematrix cannot handle 3d.
Adam Johnston
Adam Johnston le 2 Juil 2021
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.

Connectez-vous pour commenter.

Réponse acceptée

Adam Johnston
Adam Johnston le 2 Juil 2021
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')

Plus de réponses (0)

Produits


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by