Question

I'm reading in a csv file of time-series data into a C++ program. My data however contains some NaN's. For example:

1-Jul-2010,   1.0 
2-Jul-2010,   2.0
3-Jul-2010,   NaN
4-Jul-2010,   3.0

To deal with this I wrote a short script in Matlab which replaces all the NaN's with 0.0 - I then read in the new file without the NaN's. Is there an easy way or avoiding this pre-processing?

Thanks!

Was it helpful?

Solution

As David Given already mentioned, you do not have to pre-process the file at all. strtof() and strtod() are both able to convert the NaN string to the NaN float/double value.

If you want to replace the values with 0.0 in your dataset, you can do so using the isnan() function.

if (isnan(val))
{
    val = 0.0;
}

OTHER TIPS

Do you want the result to be a float (or double) NaN - then use the stdlib strtod() function.

If you want to deal with it in your own way (set it to 0 etc) then read each line into a string first, check for NaN with strcmp() then parse the string depending on what you find - easier if NaN can only appear in one column.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top