Borrar filtros
Borrar filtros

Writing nan values to text files

5 visualizaciones (últimos 30 días)
Mark
Mark el 2 de Feb. de 2012
Editada: Matt J el 29 de Sept. de 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

Respuestas (1)

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

Iniciar sesión para comentar.

Categorías

Más información sobre Printing and Saving en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by