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.
Geoff Hayes
Geoff Hayes 2021년 12월 15일
@Pappu Murthy - which line of code is generating the error?
Pappu Murthy
Pappu Murthy 2021년 12월 15일
The last line is giving error.

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

 채택된 답변

Image Analyst
Image Analyst 2021년 12월 15일

0 개 추천

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

Pappu Murthy
Pappu Murthy 2021년 12월 15일
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 2021년 12월 15일
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 2021년 12월 15일
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...
Pappu Murthy
Pappu Murthy 2021년 12월 15일
The second suggestion worked just fine. Thanks. We can close this.

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

추가 답변 (0개)

카테고리

제품

릴리스

R2021b

질문:

2021년 12월 15일

댓글:

2021년 12월 15일

Community Treasure Hunt

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

Start Hunting!

Translated by