Read in a txt file with integers and string
조회 수: 5 (최근 30일)
이전 댓글 표시
I don't think this question is too hard but I've really been struggling getting matlab to import my text file so I hope you all are able to help me!
Below is an example of the first few lines of the file that I manually put into excel to better show the data. The headers are not in the text file but are a description of the data in the columns.
I've had trouble reading this in even when I only try and read in the data with the first 3 columns but the besides the basics, the second to last string (highlighted in the picture) is sometimes not present and I'm not sure how to handle that so it will just be blank or changed to a 0 when I import the data.

Any help you all could give me would be greatly appreciated!
Thank you!
채택된 답변
Stephen23
2015년 2월 9일
편집: Stephen23
2015년 2월 9일
textscan is your friend. If you upload a small text file of your data, I can show you how it can be configured to work with your data.
Given your image of the excel spreadsheet, I came up with this:
fid = fopen('temp.txt','rt');
C = textscan(fid,'%d%d%s%s%d%f%s%s%s','Delimiter','\t','Whitespace','');
fclose(fid);
This returns me a 1x9 size cell array with all of the file's data in it, for example:
>> C{6}
ans =
17.5000
17.5000
12.2500
8.5000
7.8750
>> C{9}
ans =
'HC606'
'HCM605'
'HCR408'
'DP0768'
'DP506Z'
The data file is attached. Feel free to use it to test this code with.
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Spreadsheets에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!