read part of a .txt file
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Voss
2024-8-20
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 个评论
更多回答(1 个)
Voss
2024-8-20
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)
另请参阅
类别
在 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!