What is the best option to import individual columns from text files?

1 visualizzazione (ultimi 30 giorni)
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.

Risposte (1)

Mathieu NOE
Mathieu NOE il 7 Giu 2022
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

Prodotti

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by