Read .txt file into a matrix and remove unwanted text
조회 수: 5 (최근 30일)
이전 댓글 표시
Hello,
I have a .txt file that I want to simplify into a matrix. I want to remove the first 3 lines completely, keep the numbers in the first row without the decimal places or colon (i.e. 6248.2: should become 6248) and I want to keep the key words in the last column such as flush-evac, flush, etc. I would also like the remove the line in the middle of the file that starts with a #. I'm pretty sure that textscan can accomplish this task but I am struggling with acquiring the correct format. Any help would be greatly appreciated.
댓글 수: 0
채택된 답변
추가 답변 (1개)
Voss
2022년 1월 5일
fid = fopen('N2_trace.txt');
data = fread(fid);
fclose(fid);
c_data = strsplit(char(data).',newline());
if isempty(c_data{end})
c_data(end) = [];
end
c_data(1:3) = [];
time = cellfun(@(x)str2double(x(1:find(x == '.',1)-1)),c_data);
status = cellfun(@(x)x(find(x == ':',1,'last')+2:end),c_data,'UniformOutput',false);
disp(c_data);
disp(time);
disp(status);
참고 항목
카테고리
Help Center 및 File Exchange에서 Text Files에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!