How to export a matrix of numeric values to Excel using writematrix

19 vues (au cours des 30 derniers jours)
Carlos Acasuso
Carlos Acasuso le 7 Déc 2020
Commenté : Ameer Hamza le 7 Déc 2020
Hello,
Since Matlab no longer recommends the use of xlswrite, I am trying to use writematrix instead.
I am having the problem that, when I export a matrix of double numeric values to excel, these are exported as text, causing problems in further calculations that excel does with the data being exported. I can correct this easily in excel, but it is not efficient and this was not happening if I used xlswrite.
My line of code to export the data is:
writematrix(MaxMinStructExport.ExcelOutput', ExcelExport.Filename, 'Sheet','PRod MaxMin', 'Range', strcat('B',ExcelExport.Sess_ExRow))
Where my file name is of the format:
ExcelExport.Filename = 'Example.xlsx';
Is there any quick solution to this that I am missing? I've attempted the use of the name-value pair 'PreserveFormat', but this is giving me an error as an invalid parameter name.
Also, as a side question, I'd be curious on why xlswrite is not recommended by Matlab, and if as users we should avoid its use as it's likely to become obsolete in future Matlab releases.
Thanks in advance for your help.
Carlos
  2 commentaires
Ameer Hamza
Ameer Hamza le 7 Déc 2020
No, currently, there is no plan to remove xlswrite as written in the compatibility considerations section of xlswrite: https://www.mathworks.com/help/releases/R2020b/matlab/ref/xlswrite.html#mw_88a93783-cc81-4dbc-8146-46289887a551.
Also, what is the class of MaxMinStructExport.ExcelOutput.
Carlos Acasuso
Carlos Acasuso le 7 Déc 2020
Thank you Ameer.
Why is it not recommended for newer versions of Matlab?:
xlswrite is not recommended
Not recommended starting in R2019a
MaxMinStructExport is a 128x34 string
Looks like this:
As you can see, there is numeric values, which come from double numeric arrays, and also some string values, which cause MaxMinStructExport to be a string. I guess I'm understanding the problem now, and the only way of using writematrix would be to either separate the matrices being exported (one for string values, one for numeric) or use a cell to store the data instead. What is your opinion?
However, if I just use xlswrite instead, then even if MaxMinStructExport is a string, the numeric values are detected as numbers by excel, not text.
Thanks,
Carlos

Connectez-vous pour commenter.

Réponse acceptée

Ameer Hamza
Ameer Hamza le 7 Déc 2020
The documentation mentions that the newer functions have better cross-platform support and performance as compared to xlswrite.
As you correctly identified, if the matrix itself is a string, the writematrix() will save them as a string too (I think this behavior makes more sense, instead of automatically converting to numeric values, without informing the user). Creating a cell array and using writecell() will be the correct approach here; otherwise, you can keep using xlswrite() if it is working fine.
The other approach is to make two calls to writematrix() like this
writecell(string_rows_as_cell_arrays)
writematrix(numeric_matrix)
  2 commentaires
Carlos Acasuso
Carlos Acasuso le 7 Déc 2020
Thank you Ameer, very useful.
I am going to leave for now the xlswrite function as it works well and makes it simpler.
I have found that sometimes the writecell, writematrix functions can be a bit time-consuming, so I would like to stay away from doing the export in two lines of code if possible.
Thanks,
Carlos
Ameer Hamza
Ameer Hamza le 7 Déc 2020
Yes, I/O operations are usually bottlenecked in most computing tasks. Two calls, in this case, will definitely be slower. xlswrite() will be correct in this case.

Connectez-vous pour commenter.

Plus de réponses (0)

Produits


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by