writematrix resizing column widths

45 views (last 30 days)
James
James on 3 Dec 2020
Answered: Robert on 7 Sep 2022
I'm using Matlab 2020a. When I use the function writematrix to write data to MS Excel, it changes the column widths. This is obnoxious. How do I tell it to not do that? ActiveX is too much work and overkill for what I need. Is the simple work around just to keep using xlswrite? Yes, I know MathWorks recommends using writematrix etc. over xlsread. Why? xlsread does exactly what I want without changing things I don't want changed.
Mathworks: don't recommend something that doesn't work as well as what it replaces.
  1 Comment
Ron Fredericks
Ron Fredericks on 26 Apr 2022
I am using writematrix() in my code. I had the same problem James describes above - column width uncontrollable and oft times too narrow.
I found a 'nice' workaround:
For me, titles for columns would end up truncated because cell widths (columns) were optimized for the data which took less space. The solution was to write the titles last, after all data was written. Titles are still at the top of the column for me, but now the width is adjusted for titles.
In other words:
Write your longest data cells last - even if those data elements show at the top of the excel worksheet page. The column of data cells will now be formatted to fit the data requiring the widest width.

Sign in to comment.

Answers (2)

Fangjun Jiang
Fangjun Jiang on 3 Dec 2020
Look for the 'PreserveFormat' and the 'UseExcel' properties.
Also, look for the writecell() function.

Robert
Robert on 7 Sep 2022
For anyone encountering this problem and using R2020b, set the AutoFitWidth name-value pair for writematrix to false. Unfortunately, this does not help people using R2020a and earlier. The PreserveFormat and UseExcel name-value pairs Fangjun suggests do not help in that case.

Products

Community Treasure Hunt

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

Start Hunting!

Translated by