Writing nan values to text files
7 ビュー (過去 30 日間)
古いコメントを表示
Hi,
Is there any quick way to ask Matlab to always save nan values as "NaN" instead of "nan"? I use export (for saving datasets) and dlmwrite (for everything else).
Ideally, I would be able to set some global setting for this, but if that's not possible, I guess I will need to modify each call to export and dlmwrite, but I'm not sure how.
Thanks in advance
0 件のコメント
回答 (1 件)
Walter Roberson
2012 年 2 月 2 日
Sorry, dlmwrite() has no provision for this. Consider post-processing the output file, such as with perl or sed.
2 件のコメント
Walter Roberson
2012 年 2 月 4 日
perl is pretty fast, and the perl command is easy to write.
In Unix, at the shell command prompt, it would be
perl -pe 's/nan/NaN/g' < OldFile > NewFile
You could also create a file chnan.pl containing
--- chnan.pl below here -- do not include this line
#!perl
$infile = shift;
$outfile = shift;
open(my $infh, "<", $infile) or die "cannot open input $infile: $!";
open(my $outfh, ">" $outfile) or die "cannot open output $outfile: $!";
select($outfh);
while (<$infh>) { s/nan/NaN/g; print }
close($outfh);
close($infh);
--- chnan.pl above here -- do not include this line
Then, inside of MATLAB itself, you could invoke
perl('chnan.pl', 'OldFile', 'NewFile');
In the above discussion, OldFile should be replaced by the name (with extension) of the text file that you saved in to, and NewFile should be replaced by the name (with extension) of the text file you want to write the modified content to. Do NOT make the input and output file names the same!
The exact name chnan.pl is not important, but you should be sure to use an extension such as .pl (the standard extension for Perl) that does not conflict with anything used by MATLAB (e.g., .m, .p)
参考
カテゴリ
Help Center および File Exchange で Text Files についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!