WriteMode append not working when actually appending?

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 comentarios

Hello
can you share the code and data to replicate the issue ?
Adam Johnston
Adam Johnston el 2 de Jul. de 2021
Editada: Adam Johnston el 2 de Jul. de 2021
Hi,
The exact code is
%Print date and time to output file
writematrix(datetime,strcat('OutputMatrices\',string(N),'modes.xls'),'WriteMode','append');
%Print data to output file
writematrix('Witnesses',strcat('OutputMatrices\',string(N),'modes.xls'),'WriteMode','append');
writematrix(warray,strcat('OutputMatrices\',string(N),'modes.xls'),'WriteMode','append');
and a few nearly identical lines. N is an integer. warray is a 3D 2N*2N*j matrix with j another integer. A specific example could have
N=3;
warray=cat(3,eye(6),zeros(6),magic(6));
I do however get the same error when using the code in the question.
writematrix cannot handle 3d.
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.

Iniciar sesión para comentar.

 Respuesta aceptada

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')

Más respuestas (0)

Productos

Versión

R2021a

Preguntada:

el 1 de Jul. de 2021

Respondida:

el 2 de Jul. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by