Export Data via fprintf printing something else besides NaN

6 Ansichten (letzte 30 Tage)
Brian
Brian am 25 Nov. 2013
Kommentiert: Brian am 25 Nov. 2013
Hi, I have a simple export loop that is writing the contents of a matrix to a text file for another system to read. The issue is that the other system doesn't like NaN as part of a numeric array and I need to print a blank space instead. How can I go about doing this?
On occasion my MacroData matrix Contains NaN's. Is there a way for me to do a find replace while I'm in the file itself, or simple replace the NaN output with a blank?
f_format = '%s%f%f\r\n';
count = 1;
for i = 1:length(UniqueDates)
fprintf(ftemp,f_format,datestr(UniqueDates(i,1),'mm/dd/yyyy'),MacroData(i,:));
end
fclose(ftemp);
Thanks a lot, Brian

Akzeptierte Antwort

Titus Edelhofer
Titus Edelhofer am 25 Nov. 2013
Hi,
a simple solution would be to print first into a string, process the string and then dump to the file, something like
for i = 1:length(UniqueDates)
str = sprintf(f_format,datestr(UniqueDates(i,1),'mm/dd/yyyy'),MacroData(i,:));
str = strrep(str, 'NaN', ' ');
fprintf(f_temp, '%s', str);
end
Titus
  1 Kommentar
Brian
Brian am 25 Nov. 2013
Works perfect thanks Titus. I had never used sprintf before so I really wasn't certain of it's use.
Thanks again, Brian

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Characters and Strings 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