Why is the leading zero dropped when I write a string of numbers to Excel?
4 次查看(过去 30 天)
显示 更早的评论
I am trying to write a date string to an Excel file. I use the following command to create a date string:
a = {datestr(date, 'yymmdd')} % date in format yymmdd
This returns a 1x1 cell containing the string '051019', for example, which has a leading zero.
If I write this to an Excel file with the following command:
xlswrite('a.xls', a)
cell A1 of the spreadsheet contains the following:
51019
The leading zero has been dropped.
采纳的回答
MathWorks Support Team
2009-6-27
This behavior is caused by the way in which Excel discards leading zeros in numbers. To work around this issue, place a single quote before the string, which specifies to Excel that the following value should be entered as text. For example:
a = {['''',datestr(date, 'yymmdd')]}; % concatenate a single quotation to date string
xlswrite('a.xls',a);
This approach does not preclude Excel's ability to perform mathematical operations on the inserted data.
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Spreadsheets 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!