Problems with te output of writetable function

writetable(E,'FinalMatlab.csv')
I ended up with the attached output. in row 33 the columns were not written completely and instead were written on the next row (before ID 34). Also, it would be great if NaT and Nan could be replaced by empty cells.
Any help would be greatly appreciated

댓글 수: 3

It would be easier if we had the .mat containing the table being written.
I suspect that some of the fields have either carriage returns or line feeds.
Wesso
Wesso 2019년 3월 4일
편집: Wesso 2019년 3월 4일
thanks Walter. Attached the first 35 rows in the mat file and I kept in the mat file the names of real variables used in case they were the source of problem
Wesso
Wesso 2019년 3월 4일
sorry the initial file is too big to upload it . so I took the first 35 rows

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

 채택된 답변

Guillaume
Guillaume 2019년 3월 4일

0 개 추천

Walter was correct, some of your text fields contain newlines. So, of course, when the newlines are written it messes up the file for some csv readers. Note that any entry with newlines are enclosed in double quotes, so if the parser works detects quoted strings properly it should read the file correctly.
Probably best, is to get rid of these newlines characters. An easy way:
yourtable(:, vartype('cellsttr')) = varfun(@(v) regexprep(v, '[\n\r]', ''), yourtable(:, vartype('cellstr')));
writetable(yourtable, 'something.csv');
You might similarly want to get rid of commas within your text (or replace them with something else), if it's going to confuse your reader.

댓글 수: 3

Thanks Guillaume
As you suggested:
E(:, vartype('cellsttr')) = varfun(@(v) regexprep(v, '[\n\r]', ''), E(:, vartype('cellstr ')));
I am receiving: Error using vartype (line 43)
Invalid data type. Input must be a string or a character vector specifying a valid class name.
Oops, typo, should be 'cellstr' not 'cellsttr'. It's spelled correctly in the 2nd occurence.
I would probably replace with a space rather than with emptiness so that adjacent words do not run together. Or I would replace with '\\n' to put literal character pair \n as the replacement, expecting you to transform them back upon reading.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Cell Arrays에 대해 자세히 알아보기

태그

질문:

2019년 3월 3일

댓글:

2019년 3월 4일

Community Treasure Hunt

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

Start Hunting!

Translated by