why "writetable" replaces points with commas ???

27 views (last 30 days)
i have this array (12541x763),
and when i use writetable i will get this result
it replaces the points with commas, i wanted to keep the points instead of replacing them with commas,
this is the code I am using :
T = cell2table(DD);
writetable(T, 'myData.xlsx', 'WriteVariableNames', 0)
Peter Perkins
Peter Perkins on 29 Jul 2021
I suspect that your beef is with Excel, not with MATLAB. writetable is not writing out text, this is a spreadsheet. It's writing out binary floating point.
Walter Roberson
Walter Roberson on 30 Jul 2021
this is a spreadsheet. It's writing out binary floating point.
No, it is xlsx, which is zip'd XML files. The numbers are represented as text.
tn = tempname() + ".xlsx"
tn = "/tmp/tp108c97b6_2ccc_4b5b_96bc_165c1e6d1555.xlsx"
SomeVar = [-29.7073; -29.7173734]
SomeVar = 2×1
-29.7073 -29.7174
T = table(SomeVar)
T = 2×1 table
SomeVar _______ -29.707 -29.717
writetable(T, tn)
cleanme = onCleanup(@() delete(tn));
[folder, filename, ext] = fileparts(tn)
folder = "/tmp"
filename = "tp108c97b6_2ccc_4b5b_96bc_165c1e6d1555"
ext = ".xlsx"
outdir = fullfile(folder, filename);
unzip(tn, outdir)
cleanme2 = onCleanup(@() rmdir(outdir, 's'));
S = fileread(fullfile(outdir, 'xl', 'worksheets', 'sheet1.xml'));
regexprep(S, '>', '>\n')
ans =
'<?xml version="1.0" encoding="UTF-8"?> <worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"> <dimension ref="A1:A3"/> <sheetFormatPr defaultRowHeight="15"/> <cols> <col min="1" max="1" width="9.140625" customWidth="true"/> </cols> <sheetData> <row r="1"> <c r="A1" s="0" t="s"> <v> 0</v> </c> </row> <row r="2"> <c r="A2" s="0"> <v> -29.7073</v> </c> </row> <row r="3"> <c r="A3" s="0"> <v> -29.7173734</v> </c> </row> </sheetData> </worksheet> '
Notice the text -29.7073 and the text -29.7173734

Sign in to comment.

Answers (2)

Stephen23 on 30 Jul 2021
Edited: Stephen23 on 30 Jul 2021
"why "writetable" replaces points with commas ???"
It doesn't.
The Open Office XML format (e.g. XLSX) uses only the decimal point within the XML files, regardless of the locale settings (and similarly all functions are actually stored in English, not in the language you might see displayed by Excel on your screen). WRITETABLE correctly creates the files according to the OOXML standard.
The comma you show in your second screenshot is purely an artifact of how the file is displayed in MS Office Excel according to the locale settings on your computer (tip: you can change the OS locale setting for the decimal radix character).
How MS Office Excel displays the decimal radix character has absolutely nothing to do with MATLAB.

Jeremy Hughes
Jeremy Hughes on 30 Jul 2021
@Walter Roberson, @Peter PerkinsYou're both right. Walter is more right.
An XLSX file is a zip file containing XML data which is what the XLSX file is defined as. And as @Stephen Cobeldick points out, the text in the internal XML file is written out with "." as the decimal separator. (You can rename .xlsx files with .zip extenion and extract to see what's literally stored, but it's not easy to understand). Excel renders those number based on the local it sees fit.
If you call readtable on the created file, you're going to see the same numbers come back.
Peter Perkins
Peter Perkins on 30 Jul 2021
Walter is right, I'm just out of date.
Walter Roberson
Walter Roberson on 30 Jul 2021
The answer would have been different for a .xls file !

Sign in to comment.


Community Treasure Hunt

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

Start Hunting!