How to convert a .txt to a .xls?

조회 수: 2 (최근 30일)
Matt
Matt 2011년 6월 11일
I have many tab delimited .txt files that contain text and numerical values. I need to convert these to .xls files using a matlab script. I have looked into dlmread, txtscan, etc. The problem is that it contains both text and numbers, and also that it is not a perfect table. It has some random text and numbers above the actual data. I need it to be universal so it can convert any tab delimited text file into a .xls regardless of format. So really all I need to know is how to read this type of file into Matlab. Is this even possible in Matlab? Thanks!
-Matt

답변 (2개)

Walter Roberson
Walter Roberson 2011년 6월 11일
fopen() the file. Start a loop. fgetl() a single line from the file. If that fails, break out of the loop. regexp() with 'split' on '\t' to split the line in to a cell string of individual fields. Loop checking all of the fields. If any character in the field is not '-', '+', 'e', 'E', 'd', 'E', '.', a leading space, a trailing space, or one of the digits, then the field is definitely a string so store it; otherwise try to str2double() the field, and if that works store the floating point value and otherwise store the string. End of loop over the fields. Save the result of converting the fields of the line. End loop over the lines.
You know have a cell array of vectors of cell arrays. To convert it to .xls, you are going to have to find the longest of the lines and pad the other lines with empty fields. Once you have all of the lines the same, length, you can convert to a cell array matrix. Once you have that, submit it to xlswrite()

Fangjun Jiang
Fangjun Jiang 2011년 6월 11일
If you don't need to process the data, there might be a different way to do it. I can open a delimited .txt file directly with Excel. All the delimited data are right in the cells of the spread sheet. You can test it manually. If that is what you want, then you can use Matlab to make this automation so it can convert multiple .txt files. Look into the Excel actxserver, all you need to do is open the text file and then save as Excel file.
  댓글 수: 1
Cordelle
Cordelle 2013년 6월 18일
@Fangjun Jiang
How can I do the automation of open a .txt file directly with Excel through matlab?

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

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by