search for a string in other file and return new files based on multiple condiion
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
sensation
am 19 Okt. 2017
Kommentiert: sensation
am 20 Okt. 2017
Hi and I hope you can give me some advice or suggestion,
I have bunch of files and what I would like is to create new files based on a condition. For example my input files are curva_pbc_uof_20150103.txt and curva_pbc_uof_20150104.txt and condition is in cond.txt file.
I would like to create new files where fourth column of my files matches conditions, so if I have ABO1 in curva_pbc_uof_20150103.txt and ABO1 in cond.txt, I should keep those rows, and in addition only those rows where the last column is equal to C. All other rows should be neglected.
Thanks a lot!
0 Kommentare
Akzeptierte Antwort
Andrei Bobrov
am 19 Okt. 2017
Bearbeitet: Andrei Bobrov
am 19 Okt. 2017
n = dir('curva_pbc_uof_*.txt');
nn = {n.name}';
k = numel(nn);
c = readtable('cond.txt','ReadVariableNames',false);
c = regexp(c.Var1,'\w*','match','once');
T_out = cell(k,1);
for ii = 1:k
T = readtable(nn{ii},'delimiter',';','DatetimeType','text','ReadVariableNames',true);
if ii == 1
a0 = T.Properties.VariableNames;%if ii = 1
a1 = a0(6:7);
end
T = T(:,~strncmp(a0,'Var',3));
T.Fecha = datetime(T.Fecha,'F','dd/MM/uuuu');
T{:,a1} = regexprep(T{:,a1},{'\.',','},{'','\.'});
b = array2table(str2double(T{:,a1}),'v',a1);
T(:,a1) = [];
T(:,a1) = b;
T_out{ii} = T(ismember(T.Unidad,c) & strcmp(T.Ofertada_O__Casada_C_,'C'),:);
end
9 Kommentare
Andrei Bobrov
am 20 Okt. 2017
Bearbeitet: Andrei Bobrov
am 20 Okt. 2017
c = unique(c);
[lo,ii] = ismember(T_last{:,2},c);
[g1,dat] = findgroups(T_last(:,1));
n = strcat('v_',c(:)');
sum1 = [dat,array2table(accumarray([g1,ii],T_last.sum,[],[],nan),'v',n)];
max1 = [dat,array2table(accumarray([g1,ii],T_last.max,[],[],nan),'v',n)];
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Data Type Conversion finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!