how can i export a numeric vector that starts from 0 eg. 0001 from matlab to excel?

2 ビュー (過去 30 日間)
ccc7
ccc7 2019 年 2 月 11 日
コメント済み: Jeremy Hughes 2019 年 2 月 11 日
how can i export a numeric vector that starts from 0 eg. 0001 from matlab to excel?
  1 件のコメント
madhan ravi
madhan ravi 2019 年 2 月 11 日
編集済み: madhan ravi 2019 年 2 月 11 日
illustrate with an explicit example

サインインしてコメントする。

回答 (6 件)

Jeremy Hughes
Jeremy Hughes 2019 年 2 月 11 日
What's happening when you pass "0001" into a spreadsheet is the same thing that would happen if you typed that number into the spreadsheet from the Excel application. It converts the internal storage to "number". If you want to preserve exact text, add a single quote mark before the text, e.g. "'0001". (or {'''0001'} for cells.) This is the same thing you'd need to do if you tried tying the number into the Excel application to preserve the leading zeros.
This only happens on Windows when communicating with an active Excel process. If you're in the most recent release, you can also use the "UseExcel",false parameter, which won't do any post processing to the text; it is stored as-is. If you do, however, opening the file in Excel and re-saving might modify the contents.
  1 件のコメント
madhan ravi
madhan ravi 2019 年 2 月 11 日
編集済み: madhan ravi 2019 年 2 月 11 日
So I am using a Mac but the OP maybe using windows which may be the effect of this you say?

サインインしてコメントする。


madhan ravi
madhan ravi 2019 年 2 月 11 日
Use xlswrite() or writetable().
  2 件のコメント
madhan ravi
madhan ravi 2019 年 2 月 11 日
Charalambos Charalambous's answer moved here:
Hi Madhan
I do use xlswrite. The goal here is to add subject's id number (eg. 0001) in the fisrt collumn of each tab.
the code that i am using is
subjectid = cellfun(@num2str,{'0001'},'un',0)
xlswrite('temp.xlsx',subjectid)
But as you can see in the attached file, the exported value is '1'.
It would be great if i am able to export this value as '0001' from matlab to excel.
Any help would be very appreciated.
madhan ravi
madhan ravi 2019 年 2 月 11 日
Try the below:
a={'0001'}
writetable(table(a),'xxx.xlsx')

サインインしてコメントする。


ccc7
ccc7 2019 年 2 月 11 日
Hi,
Thank you for the code; however, stil the same issue. It seems that excel does accept 0s before a number. Is this true?
  1 件のコメント
madhan ravi
madhan ravi 2019 年 2 月 11 日
What do you mean? Did‘nt my comment do what you asked for ?

サインインしてコメントする。


ccc7
ccc7 2019 年 2 月 11 日
unfortunately no. see attached file. A2 cell = '1'
  1 件のコメント
madhan ravi
madhan ravi 2019 年 2 月 11 日
I haven‘t checked the excel file but the data is preserved. Try
readtable('xxx.xlsx')

サインインしてコメントする。


ccc7
ccc7 2019 年 2 月 11 日
what version do you use? I am using 2017b. Still does not work.
readtable.PNG
  3 件のコメント
Jeremy Hughes
Jeremy Hughes 2019 年 2 月 11 日
Are you running on linux or mac?
madhan ravi
madhan ravi 2019 年 2 月 11 日
On a mac Jeremy.

サインインしてコメントする。


ccc7
ccc7 2019 年 2 月 11 日
hi all,
i am using pc. I tried Jeremy's suggestion (i.e., {'''0001'} ) and works fine!!!!
You have saved me from a lengthy manual work. now it is automated.
Thank you both.

カテゴリ

Help Center および File ExchangeSpreadsheets についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by