How can I replace NaN with '#N/A' in a dataset and write it to Excel?

Hi~
I want to do some calculation and write the result to Excel. I want the null to be write as '#N/A' so that the manipulation will be easier in Excel. As an example, I write:
a=rand(3,2);
a(2)=nan;
A=dataset(a(:,1),a(:,2));
A.Properties.VarNames ={'a1','a2'};
export(A,'XLSFile','myA.xlsx')
but the null is a blank in Excel, I wonder how to replace it with '#N/A' . Any ideas?

 採用された回答

Mischa Kim
Mischa Kim 2014 年 3 月 15 日
編集済み: Mischa Kim 2014 年 3 月 16 日

0 投票

Bond, you could use
a = rand(3,2);
a(2,2) = NaN;
b = num2cell(a);
b(isnan(a)) = {'#N/A'};
bheader = {'a1' 'a2'};
filename = 'myA.xlsx';
sheet = 1;
xlswrite(filename,bheader,sheet,'A1')
xlswrite(filename,b,sheet,'A2')
Switching to a cell array allows for saving numeric and non-numeric values in the same object.

1 件のコメント

Bond
Bond 2014 年 3 月 19 日
Thank you, Kim. Your answer is very helpful.

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeData Import from MATLAB についてさらに検索

タグ

質問済み:

2014 年 3 月 15 日

コメント済み:

2014 年 3 月 19 日

Community Treasure Hunt

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

Start Hunting!

Translated by