read part of a .txt file
2 views (last 30 days)
Show older comments
I have the attached .txt file. I would like to read only the lines where the number from 1 to 200 is included.
Each row should be divided into 7 columns: iter | continuity | x-velocity | y-velocity | z-velocity | time | iter.
0 Comments
Accepted Answer
Voss
on 20 Aug 2024
filename = 'file.txt';
str = readlines(filename);
C = arrayfun(@(s)sscanf(s,'%f %f %f %f %f %d:%d:%d %f'),str,'UniformOutput',false);
C(cellfun(@isempty,C)) = [];
M = [C{:}].';
time = duration(M(:,6:8));
M(:,6:8) = [];
names = strsplit(str(1),{' ','/'});
names(strcmp(names,"")) = [];
names{end} = [names{end} '_remaining'];
T = array2table(M,'VariableNames',names([1:end-2 end]));
T = addvars(T,time,'After',names{end-2})
0 Comments
More Answers (1)
Voss
on 20 Aug 2024
filename = 'file.txt';
T = readtable(filename,'CommentStyle','R','NumHeaderLines',0,'VariableNamingRule','preserve');
T(all(isnan(T{:,:}),2),:) = [];
T.Properties.VariableNames([6 7]) = {'time','iter_remaining'};
disp(T)
2 Comments
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!