Filter löschen
Filter löschen

Writing nan values to text files

5 Ansichten (letzte 30 Tage)
Mark
Mark am 2 Feb. 2012
Bearbeitet: Matt J am 29 Sep. 2013
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

Antworten (1)

Walter Roberson
Walter Roberson am 2 Feb. 2012
Sorry, dlmwrite() has no provision for this. Consider post-processing the output file, such as with perl or sed.
  2 Kommentare
Mark
Mark am 4 Feb. 2012
That's not an easy option due to the size of the files.
Walter Roberson
Walter Roberson am 4 Feb. 2012
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)

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Printing and Saving finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by