export csv file without NaN values

7 Ansichten (letzte 30 Tage)
joseph Frank
joseph Frank am 28 Sep. 2013
Kommentiert: Elena Bertseva am 2 Jun. 2017
Hi,
I am using the following code:
filename='AllFirms.csv';
%write string to csv
fileID = fopen(filename,'wt');
[rows, columns] = size(DATAtxt);
for index = 1:rows
fprintf(fileID, '%s,', DATAtxt{index,1:end-1});
fprintf(fileID, '%s\n', DATAtxt{index,end});
end
fclose(fileID);
%write numerical data to csv
dlmwrite(filename, DATA,'-append', 'delimiter', ',');
where DATA is a numeric matrix and DATAtxt contains the header names of the columns. This code ,however, writes NaN values which causes me problems later on . How can I export these NaN values as empty ?

Antworten (2)

Azzi Abdelmalek
Azzi Abdelmalek am 28 Sep. 2013
Bearbeitet: Azzi Abdelmalek am 28 Sep. 2013
DATA(isnan(DATA))=0
  3 Kommentare
Azzi Abdelmalek
Azzi Abdelmalek am 28 Sep. 2013
% convert DATA to a cell array
DATA1=num2cell(DATA)
DATA1(isnan(DATA))={[]}
Then export DATA1
Elena Bertseva
Elena Bertseva am 2 Jun. 2017
Nice idea, but in my case, when the NaNs are on the ends of the lines and rows, dlmwrite gives: "The input cell array cannot be converted to a matrix."

Melden Sie sich an, um zu kommentieren.


Image Analyst
Image Analyst am 28 Sep. 2013
Give an example of a line where you're writing data that is nan. All I see is the first part where you write the string data but I don't know how you want the numerical data. For example, let's say DATA = [1,2,nan, 4,5]. Do you want
1,2,,4,5
or do you want
1,2,4,5
in the csv file? And why can't you just write it out a number at a time using isnan() and fprintf()? You don't have to use dlmwrite() you know.

Kategorien

Mehr zu Characters and Strings finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by