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 个评论

Jan
Jan 2021-12-15
编辑:Jan 2021-12-15
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.
@Pappu Murthy - which line of code is generating the error?
The last line is giving error.

请先登录,再进行评论。

 采纳的回答

Image Analyst
Image Analyst 2021-12-15
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 个评论

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.
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.
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.
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...
The second suggestion worked just fine. Thanks. We can close this.

请先登录,再进行评论。

更多回答(0 个)

产品

版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by