How to import odd and even rows from a more than one txt file ?
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hello;
I have 20 different txt files and the content of each of them is similar to the one I have stated below. I need to save the numbers in the odd rows of the 1st column as 'A' and the numbers in the even rows of the 3rd column as 'B'; but after the 150th row, the text part starts and I should not include this part.
Can you help me?
Thank you
txt file:
x y z
1 2 3
4 5
6 7 8
9 10
.
.
.
.
The explanation part starts after line 150th row.
0 commentaires
Réponse acceptée
Mathieu NOE
le 6 Déc 2022
Modifié(e) : Mathieu NOE
le 6 Déc 2022
hello
maybe this ?
I wasn't sure when you say even / odd rows if we take the first line (x y z) into account or not
so choose your option in the code
clc
clearvars
fileDir = pwd; % choose your working directory
start_line = 2;
stop_line = 150;
S = dir(fullfile(fileDir,'data*.txt')); % get list of data files in directory
S = natsortfiles(S); % sort file names into natural order , see :
%(https://fr.mathworks.com/matlabcentral/fileexchange/47434-natural-order-filename-sort)
for k = 1:length(S)
[A,B] = do_the_job(fileDir, S(k).name,start_line,stop_line)
end
%%%%%%%%%%%% function %%%%%%%%%%%%%%%
function [A,B] = do_the_job(fileDir,filename,start_line,stop_line)
D=readlines(fullfile(fileDir,filename)); % read as string array
D= D(start_line:stop_line,:);
% remove empty string
D(D == '') = [];
D = str2double(split(D,' '));
[m,n] = size(D);
% I need to save the numbers in the odd rows of the 1st column as 'A'
% and the numbers in the even rows of the 3rd column as 'B';
% % option 1 : "odd / even" rows without taking the first line (headerline)
% % in account
% A = D((1:2:m),1); % odd rows of the 1st column as 'A'
% B = D((2:2:m),3); % even rows of the 3rd column as 'B';
% option 2 : "odd / even" rows with taking the first line (headerline)
% in account
A = D((2:2:m),1); % odd rows of the 1st column as 'A'
B = D((1:2:m),3); % even rows of the 3rd column as 'B';
end
16 commentaires
Mathieu NOE
le 14 Déc 2022
Isn't it this way you wanted the structures be organized ?
fileDir = pwd; % choose your working directory
start_line = 2;
stop_line = 150;
S = dir(fullfile(fileDir,'F*.txt')); % get list of data files in directory
S = natsortfiles(S); % sort file names into natural order , see :
%(https://fr.mathworks.com/matlabcentral/fileexchange/47434-natural-order-filename-sort)
for k = 1:length(S)
[A,B,C,E] = do_the_job(fileDir, S(k).name,start_line,stop_line);
% size(A)
% size(B)
fie1=sprintf('b%d',k);
BS.(fie1) = B;
fie2=sprintf('a%d',k);
AS.(fie2) = A;
end
you don't need a second for loop, everything is done in the first loop
now AS is
AS = struct with fields:
a1: [75×1 double] filled with A array of first FX file
a2: [75×1 double] filled with A array of second FX file
a3: [75×1 double] filled with A array of third FX file
a4: [75×1 double] filled with A array of fourth FX file
same logic for BS :
BS = struct with fields:
b1: [75×1 double] filled with B array of first FX file
b2: [75×1 double] filled with B array of second FX file
b3: [75×1 double] filled with B array of third FX file
b4: [75×1 double] filled with B array of fourth FX file
Plus de réponses (1)
Arif Hoq
le 9 Déc 2022
try this:
textfiles = dir('*.txt');
numfiles = length(textfiles);
mydata = cell(1, numfiles);
for k = 1:numfiles
mydata{k} = readmatrix(textfiles(k).name);
end
a=[mydata{:}];
maindata=a(1:149,:);
idx=1;
b=[maindata(:,idx:idx+2); maindata(:,idx+3:idx+5); maindata(:,idx+6:idx+8)];
% odd rows of the 1st column as 'A'
A=b(1:2:end,1);
% the numbers in the even rows of the 3rd column as 'B'
B=b(2:2:end,3);
0 commentaires
Voir également
Catégories
En savoir plus sur Data Type Conversion dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!