Azzera filtri
Azzera filtri

Writing nan values to text files

5 visualizzazioni (ultimi 30 giorni)
Mark
Mark il 2 Feb 2012
Modificato: Matt J il 29 Set 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

Risposte (1)

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

Accedi per commentare.

Categorie

Scopri di più su Printing and Saving in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by