writecell command is not working.

35 views (last 30 days)
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 Comments
Geoff Hayes
Geoff Hayes on 15 Dec 2021
@Pappu Murthy - which line of code is generating the error?
Pappu Murthy
Pappu Murthy on 15 Dec 2021
The last line is giving error.

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 15 Dec 2021
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 Comments
dpb
dpb on 15 Dec 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 on 15 Dec 2021
The second suggestion worked just fine. Thanks. We can close this.

Sign in to comment.

More Answers (0)

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!