Read .txt file into a matrix and remove unwanted text
8 次查看(过去 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!