How can I write empty cells to a CSV file from MATLAB?

조회 수: 12 (최근 30일)
MathWorks Support Team
MathWorks Support Team 2022년 12월 12일
댓글: Les Beckham 2023년 1월 20일
I am trying to write a matrix to a CSV file using MATLAB. However, I would like to have several empty rows at the beginning of the file. How can I do this without using filler values or a different file type?

채택된 답변

MathWorks Support Team
MathWorks Support Team 2022년 12월 12일
Since CSV files are a type of text file, you can use empty characters, character vectors, and strings to create empty cells. Then, use the 'writecell' function to write the empty cells and data matrix to a CSV file.
For example, to create a CSV file "data.csv" with the following contents:
,,,
,,,
1,2,3
4,5,6
You may use the following code:
data1 = [1 2 3];
data2 = [4 5 6];
c = {''; ''; data1; data2};
writecell(c,'data.csv')
  댓글 수: 1
Les Beckham
Les Beckham 2023년 1월 20일
For what it's worth, data.csv generated by this example will actually look like this (as expected since there are three columns). Note only two commas in the first two rows (same as the other rows).
,,
,,
1,2,3
4,5,6

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Text Files에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by