Read .txt file into a matrix and remove unwanted text
5 views (last 30 days)
Show older comments
Justin Rosenthal
on 5 Jan 2022
Commented: Justin Rosenthal
on 5 Jan 2022
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 Comments
Accepted Answer
More Answers (1)
Voss
on 5 Jan 2022
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);
See Also
Categories
Find more on Text Files in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!