writecell command is not working.

31 次查看(过去 30 天)
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 个评论
Geoff Hayes
Geoff Hayes 2021-12-15
@Pappu Murthy - which line of code is generating the 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 个评论
dpb
dpb 2021-12-15
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 个)

类别

Help CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by