writematrix resizing column widths

42 次查看(过去 30 天)
James
James 2020-12-3
回答: Robert 2022-9-7
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 个评论
Ron Fredericks
Ron Fredericks 2022-4-26
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.

请先登录,再进行评论。

回答(2 个)

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

Robert
Robert 2022-9-7
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.

产品

Community Treasure Hunt

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

Start Hunting!

Translated by