How can I change the decimal separator when exporting to Excel?

118 visualizaciones (últimos 30 días)
Johannes
Johannes el 28 de Mzo. de 2024
Editada: Stephen23 el 8 de Ag. de 2024
In my Matlab App (App Designer) I am reading an Excel file to show the data in a UITable. The Excel formatting is european, so the decimal separator is a comma. As Matlab reads the Excel sheet it interprets the comma correctly as separator. The UITable in the app shows only dots instead of commas, but thats fine. I also have some numeric Edit fields that are filled with calculation results, with a dot as decimal separator (as usual in Matlab). So the app surface looks clean and consistent with dots as decimal separators only.
The problem starts with the export of the data in a prepared Excel sheet, thats also in european format:
I would like to keep the dot as separator overall, but it keeps writing different separators in the file. The calculation results from the numeric fields are stored as a number with a dot as separator. But the UITable data is - again - with a comma. First I exported this directly after importing it from the other Excel, as its not changed in the callback. Then I tried to export the Data of the table in reference to app.UITable.Data hoping this would use the dot as separator. This did not work.
I am using the writematrix command
  6 comentarios
Dyuman Joshi
Dyuman Joshi el 2 de Abr. de 2024
Movida: Dyuman Joshi el 2 de Abr. de 2024
You could replace the commas with dots using replace and then save the data.
Stephen23
Stephen23 el 2 de Abr. de 2024
Editada: Stephen23 el 8 de Ag. de 2024
Your question does not specify the file format, so I will presume XLSX.
"The Excel formatting is european"
That is not how Excel's XLSX formatting works. For numeric data, MS Excel displays the decimal comma/point based on the locale settings of your OS: e.g. if you open an XLSX file on a computer with decimal comma then it will show the numeric data with decimal commas. It can also change the date formats, function names, and many other things to suit the locale: these things are not saved in the XLSX file itself. It is one of the best features of MS Excel.
You will really get yourself confused if you try to save numeric data with a particular decimal character (if you really must: the only practical approach is to convert the numeric data to text).
"How can I change the decimal separator when exporting to Excel?"
The simple answer is don't. Just save the numeric data as numeric.
And then use your locale settings to change how numeric data are displayed in Excel.

Iniciar sesión para comentar.

Respuestas (2)

Alexander
Alexander el 2 de Abr. de 2024
Excel uses the operating system defaults. To change this open a file in Excel and click on the windows butten (top left). Go to Excel Options (bottom, 2nd from right) go to advanced, there you should see a checkbox "Deliminter from OS". Uncheck it and put your delimiter in.
To be honest: I never used this option and I don't know whether it's working accoring your intention. I'm working with an old Excel (2007) and an old OS (Win7). Hence, I'm not sure if it's the same in a modern Excel.
Also my Excel is in German. It might be possible, that my translations of the steps above are not perfect.

Johannes
Johannes el 2 de Abr. de 2024
Editada: Johannes el 2 de Abr. de 2024
I just found my mistake. In the export of the calculation results I combined numeric and text results in one matrix. That's why Excel saved some numbers as text and made it impossible to format it as a number. I separated the text results from the numeric results and exported them individually. Now the decimal separator is uniform over the whole spreadsheet and the numbers are correctly formatted as numbers. So when a user with european system preferences opens the sheet, it's all commas. With US/UK settings it is all dots. That's how it should be. Thank you everyone for your answers and your time

Productos


Versión

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by