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!