Thread Subject: non-printing NaN's

Subject: non-printing NaN's

From: William Slater

Date: 9 Jun, 2009 16:53:02

Message: 1 of 2

Hey all:

I've got a matrix I'm trying to print to a CSV file. Some of the entries that this matrix represents have no recorded values for certain points in time, so I had those entries populated with NaN's. Here's what I'm doing so far to print to a CSV:

    data = predetermined data from ...
        sources with NaN's for blank spots; % size = m x n
    Time = predetermined points of ...
        time for data; % size = n x 1
    Output = [datestr(Time, 13), ... % time column
                   repmat(',', length(Time), 1), ... % comma to separate time
                   num2str(data, '%g, '), ... % data columns
                   repmat(char(10), length(Time), 1)] % carriage return
    fid = fopen('test.csv', 'w');
    fprintf(fid, '%s', Output');
    fclose(fid);

What I've got works just fine, but the people that want this CSV don't want to see NaN's. Is there a way to get num2str to suppress printing NaN and get it to just print spaces instead?

TIA

Subject: non-printing NaN's

From: John

Date: 12 Jun, 2009 15:50:04

Message: 2 of 2

Try this -
x=num2str(data, '%g, ');
y = regexprep(x,'nan,', ' ','ignorecase');

Now plug in 'y' when building your 'Output' variable at the place you add the data columns.

~S

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
regexprep John 12 Jun, 2009 11:54:03
nonprinting William Slater 9 Jun, 2009 12:54:05
num2str William Slater 9 Jun, 2009 12:54:05
suppress William Slater 9 Jun, 2009 12:54:05
nan William Slater 9 Jun, 2009 12:54:05
rssFeed for this Thread

Contact us at files@mathworks.com