フィルターのクリア

writing into excel sheets while keeping the leading zeros

4 ビュー (過去 30 日間)
SSG_newbiecoder
SSG_newbiecoder 2018 年 3 月 14 日
コメント済み: SSG_newbiecoder 2018 年 3 月 14 日
Hello, is there any way to keep the leading zeros in my number while saving to excel sheet as a csv file? I should be getting 000 001 010 011 etc but in the excel sheet it is 0 1 10 11. How can I solve this?
  2 件のコメント
Adam
Adam 2018 年 3 月 14 日
Isn't that down to the Excel formatting if it is a number rather than a string?
SSG_newbiecoder
SSG_newbiecoder 2018 年 3 月 14 日
even if I give it as a string it is still the same

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

採用された回答

Pawel Jastrzebski
Pawel Jastrzebski 2018 年 3 月 14 日
編集済み: Pawel Jastrzebski 2018 年 3 月 14 日
Following the:
you could use a similar method:
val1 = randi(1000,[20 1]);
val2 = rand(20,1);
val = val1.*val2
valWithZeros = num2str(val,'%09.3f')
valWithZeros = cellstr(valWithZeros)
for i=1:length(valWithZeros)
valWithZeros{i} = strjoin({'''',valWithZeros{i}});
end
xlswrite('file.xlsx',valWithZeros)
Or pre-format your sheet and then copy the data to it from Matlab. If you need to repeat this action over many batches of data, make sure you first make a copy of the pre-formatted sheet and then overwrite it with Matlab:
Pre-formatted sheet:
The Matlab code:
val1 = randi(1000,[20 1]);
val2 = rand(20,1);
val = val1.*val2
xlswrite('file.xlsx',val,'Sheet2','B2:B21')
Output:
  1 件のコメント
SSG_newbiecoder
SSG_newbiecoder 2018 年 3 月 14 日
Thank you for the help. It works for me

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeOperating on Diagonal Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by