필터 지우기
필터 지우기

Writing nan values to text files

조회 수: 5 (최근 30일)
Mark
Mark 2012년 2월 2일
편집: Matt J 2013년 9월 29일
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

답변 (1개)

Walter Roberson
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
Mark
Mark 2012년 2월 4일
That's not an easy option due to the size of the files.
Walter Roberson
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 CenterFile Exchange에서 Printing and Saving에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by