Industrademic
Industrademic

Reputation: 127

Why does appending values mixed with empty cells to a csv using dlmwrite result in too few columns?

I'm using Matlab 2012a to append a <1x19 cell> to a CSV file using dlmwrite. The cell array includes 17 numbers and 2 blanks. The result: 17 values written to the target csv file, but the two blanks missing. I am using the codes below:

    Output=num2cell([var1,var2,var3....var19]);
    Output(cellfun(@isnan,Output)) = {[]};
    dlmwrite('Target.csv',Output_m,'-append');

When I run the codes in command window I can see the blanks appear in the Output:

    Output = 

    Columns 1 through 10

    [19]    [2]    [137.5994]    [0]    []    [501.3610]    [38.4230]    [0]    [0]    [4.9160]

    Columns 11 through 19

    [4.9160]    [38.4230]    [0]    [38.4230]    [501.6580]    []    [-1.2590]    [0]    [0]

But when appended to the csv file, the blanks disappear.

Thanks.

Upvotes: 0

Views: 219

Answers (1)

SheetJS
SheetJS

Reputation: 22925

This is laid out in the documentation:

dlmwrite(filename,M) writes numeric data in array M to an ASCII format file, filename, using the default delimiter (,) to separate array elements. If the file, filename, already exists, dlmwrite overwrites the file.

Non-numeric data is ignored, which is why the data is lost.

One way of resolving the issue is to write a number like -Inf and then use a post-processor to remove those elements

Upvotes: 1

Related Questions