What is the best option to import individual columns from text files?
1 次查看(过去 30 天)
显示 更早的评论
What is the best option to import individual columns from text files? If I have let's say 100 text files in which there are matrices (where the columns are divided by a semicolon) and let's say I want to import from each file from 1 to 50) the 3rd column, and from 51 to 100 the 7th column Additionally I would like to save all these columns as an array.
回答(1 个)
Mathieu NOE
2022-6-7
hello
see suggestion below
you can expand on this code to add a condition statement on the file counts to switch from 3rd to 7th column once you pass the 50th file threshold
% read current filenames in folder
S = dir('**/*.txt');
S = natsortfiles(S); % natsortfiles now works on the entire structure
% natsortfiles available from FEX :
% https://fr.mathworks.com/matlabcentral/fileexchange/47434-natural-order-filename-sort?s_tid=srchtitle
figure(1),hold on
for k = 1:numel(S)
S(k).name % display file name in command window (for info only, you can remove / comment this line)
F = fullfile(S(k).folder, S(k).name);
%S(k).data = load(F); % or READTABLE or whatever.
out = readmatrix(F ,"NumHeaderLines", 1);
S(k).data = out(:,13); % this store the 13th column
% plot (for fun)
legstr{k} = S(k).name; % legend string
plot(S(k).data);
end
legend(legstr);
% % Take a look in the structure S: it contains all of your file data and the corresponding filenames, just as you require.
% % For example, the 2nd filename and its data:
% S(2).name
% S(2).data
0 个评论
另请参阅
类别
在 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!