writecell command is not working.

When trying to write cell with only entity it does not work. if I have more than one entity it works fine.
For e.g.
Hdr1 = {'Test_1','Test_2'};
Hdr2 = {'Test_1','Test_2','Test_3'};
Fn = "Testing.xlsx";
writecell(Hdr1,Fn,'WriteMode','Append')
writecell(Hdr2,Fn,'WriteMode','Append')
the above code works just fine and the output excel sheet has two rows.
However the same code with one change:
Hdr1 = {'Test_1'};
Hdr2 = {'Test_1','Test_2','Test_3'};
Fn = "Testing.xlsx";
writecell(Hdr1,Fn,'WriteMode','Append')
writecell(Hdr2,Fn,'WriteMode','Append')
the above does not work and gives error. The only difference I see is that Hdr1 has only one element instead of two elements.
Error:
Error using writecell (line 195)
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.

3 comentarios

Jan
Jan el 15 de Dic. de 2021
Editada: Jan el 15 de Dic. de 2021
In which of the two writecell command does the error occur? Has the file been deleted before?
You cannot define a Range in the WriteMode 'append', so the message is misleading.
Geoff Hayes
Geoff Hayes el 15 de Dic. de 2021
@Pappu Murthy - which line of code is generating the error?
Pappu Murthy
Pappu Murthy el 15 de Dic. de 2021
The last line is giving error.

Iniciar sesión para comentar.

 Respuesta aceptada

Image Analyst
Image Analyst el 15 de Dic. de 2021

0 votos

Looks like it expects the data to be inserted to have the same number of columns as the existing stuff above it. You can use xlswrite() instead. I'm pretty sure xlswrite() just blasts over existing stuff without caring about that. However you will have to call xlsread() first to find out how many rows are there already and make sure you write into the row one below the last row.

5 comentarios

Pappu Murthy
Pappu Murthy el 15 de Dic. de 2021
Everytime I try to use xlswrite, it keeps warning me that it is going to be obsolete and move over to other faunctions like readtable, writetable etc. Which is why I am trying this way. Also if I used the old fashioned way of xlswrite then I need to manually provide range by counting number of rows written etc. With writetable i can use the "Append" option. So that is my reasoning behind using this code. Oh well I already found the workaround since the code that works has more than one element in "Hdr" variable and that works fine. To me this appears to be some kind of bug and MATLAB developers should know about it so that in future versions it may be corrected. Or there is a possibility that I am totally wrong and there is a simple solution here.
Image Analyst
Image Analyst el 15 de Dic. de 2021
Well you could report it. I think they are using a different definition of append than you and it's working as designed. However your definition seems reasonable to me. I'd think maybe it should just give a warning rather than an error if the new data does not have as many columns as the existing data.
I also have a report into them about writematrix() with workbooks. Last I checked, it seemed to blow away any formatting you had in an existing workbook if you try to write to an existing workbook. Like all font sizes, cell colors, etc. were lost. Not sure if that's been fixed yet - I haven't explicitly checked that - but they didn't send me an email about it yet.
Pappu Murthy
Pappu Murthy el 15 de Dic. de 2021
Yeah these are recent additions so there are some bugs to be resolved I believe. I did complain to them and i have not heard from them yet which itself is bad news. Usually, they are able to solve my problems over phone.
dpb
dpb el 15 de Dic. de 2021
What about kludges like
Hdr1 = {'Test_1',''};
...
or
Hdr1 = {'Test_1',' '};
? First is empty second string, second just a blank, but both have two elements.
Perhaps one can fool MATLAB if not Mother Nature...
Pappu Murthy
Pappu Murthy el 15 de Dic. de 2021
The second suggestion worked just fine. Thanks. We can close this.

Iniciar sesión para comentar.

Más respuestas (0)

Productos

Versión

R2021b

Etiquetas

Preguntada:

el 15 de Dic. de 2021

Comentada:

el 15 de Dic. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by