write table without nans to txt and csv

조회 수: 48 (최근 30일)
Danielle Leblance
Danielle Leblance 2016년 12월 1일
댓글: Mustafa Fatih Çemen 2021년 11월 24일
writetable(t,'D.csv') and writetable(t,'D.txt')
are giving files with nan values. since the files are huge (around 1 million rows and 50 columns), when i replace nan with empty in these files the application crashes. the only way to deal with it is to allow matlab to write the table without nans. is it possible to do so?
  댓글 수: 2
KSSV
KSSV 2016년 12월 1일
How you have that much huge data? I don't think writing nan's would be a problem.
Danielle Leblance
Danielle Leblance 2016년 12월 2일
stata is having problem with the nans. i need the file with empty cells in order to process it with stata.

댓글을 달려면 로그인하십시오.

답변 (3개)

Peter Perkins
Peter Perkins 2016년 12월 2일
Danielle, if you can write to a spreadsheet, and then save the spreadsheet as csv, you'll get what you're looking for. Whether or not you can do that depends on what p[latform and version you're using.
You can't just "delete" the NaNs from a numeric variable, you'd have to convert to a cell array and replace cells containing NaN with empty, and you definately don't want to do that if you have a lot of data. You could however follow the lead of data analysts of days past, before NaN was a standard, and replace NaNs with something like -99 (or some other "impossible" value), save, load into Stata, and then deal with the -99's.
Hope this helps.

Md Saifuddin
Md Saifuddin 2017년 11월 28일
편집: Md Saifuddin 2017년 11월 28일
I tried many ways to do that. The conversion table2cell and reconvert cell2table is very time consuming. The fastest way to deal with this, as I found is, to save the csv with 'NaN's in it. And then openning the file as text string and replace all the 'NaN's to your desired text. In my case I removed all my NaNs.
if true
% code for 2016a
writetable(Data,char(filename),'Delimiter',',');
fid = fopen(char(filename),'rt') ;
X = fread(fid) ;
fclose(fid) ;
X = char(X.') ;
% replace string S1 with string S2
Y = strrep(X, 'NaN', '') ;
fid2 = fopen(char(filename),'wt') ;
fwrite(fid2,Y) ;
fclose (fid2) ;
end
Thanks to Jose for the replacing code portion https://www.mathworks.com/matlabcentral/answers/67052-how-to-modify-a-text-file-replacing-an-existing-string-with-an-user-defined-string#answer_118064
  댓글 수: 1
Mustafa Fatih Çemen
Mustafa Fatih Çemen 2021년 11월 24일
Hi when you replace NaN's with empty string, in text file two or more delimiters would be combind. For example "2,NaN,NaN,NaN,5" results in "2,,,,5" string, or "2,5,NaN" results in "2,5," How can we handle these situations such that they both result in 2,5 and 2,5 respectively.
Thanks.

댓글을 달려면 로그인하십시오.


KSSV
KSSV 2016년 12월 2일
You may remove nan's like below:
k = rand(100,1) ; % an array
k(randsample(1:100,10)) = NaN ; % introduce some nan's
% remove nan's
k(isnan(k))=[];
  댓글 수: 2
Danielle Leblance
Danielle Leblance 2016년 12월 2일
I received an error: Undefined function 'isnan' for input arguments of type 'table'.
KSSV
KSSV 2016년 12월 2일
For table you cannot use isnan..can you copy a part of your table?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by