필터 지우기
필터 지우기

convert .csv file to .xlsx?

조회 수: 63 (최근 30일)
ck
ck 2016년 7월 6일
댓글: Lev Vitkin 2023년 6월 29일
How can I convert a .csv file to .xlsx file in matlab?
Thank you.

답변 (2개)

Walter Roberson
Walter Roberson 2016년 7월 6일
You can use either csvread() or xlsread() to read the file, and then you can use xlswrite() to write the new file.
  댓글 수: 3
Walter Roberson
Walter Roberson 2022년 5월 6일
T = readcell(filename);
writecell(T, NewFileName)
Lev Vitkin
Lev Vitkin 2023년 6월 29일
If some cells of the .csv table are empty, then T = readcell(filename) returns the cell array where the empty cells are replaced by 'missing'. And writecell(T, NewFileName) cannot process 'missing' cells.
Use the following instead:
T = readmatrix(filename,delimitedTextImportOptions('DataLines',[1,Inf]));
writecell(T, NewFileName);

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


Azzi Abdelmalek
Azzi Abdelmalek 2016년 7월 6일
Look at this example
[~,~,a]=xlsread('your_file.csv')
b=regexp(a,',','split')
c=reshape([b{:}],numel(b{1}),[])'
xlswrite('new_file.xlsx',c)
  댓글 수: 3
Carmen Ellenberg
Carmen Ellenberg 2022년 5월 6일
Matlab always shows me a problem with the regexp funktion.
Does anybody have an idea?
Walter Roberson
Walter Roberson 2022년 5월 6일
The third output of xlsread() is a cell array, and the entries in that cell can have different data types:
  • numeric
  • character vector
  • datetime object (not sure about this one)
The third output will not be pure character vectors, not unless the file contains only text and no empty cells.
regexp() can process the character vectors, but it cannot process numeric entries.
The regexp in that code would have limited value. All that it would change would be entries that were text in the file and had quoted commas, such as
3,5,"firefly, Oxford"
The other commas such as between the 3 and 5 would already have been used to figure out the cell boundaries for xlsread.
If you do have quoted commas then it is not obvious that you want them split.
That reshape is not doing anything useful compared to just using the [b{:}]
Overall I would recommend against using the suggested code.

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

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by