writematrix resizing column widths

60 visualizzazioni (ultimi 30 giorni)
James
James il 3 Dic 2020
Risposto: Robert il 7 Set 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 Commento
Ron Fredericks
Ron Fredericks il 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.

Accedi per commentare.

Risposte (2)

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

Robert
Robert il 7 Set 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.

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by