# How to sort .txt files according to their content (number values) with an if-else-condition?

조회 수: 1 (최근 30일)
Tim K . 2022년 8월 22일
댓글: Tim K . 2022년 8월 26일
Dear all,
I have a Results.txt file, which contains a table with 13 columns with parameter-values. My goal is to loop through this table rows and give the Results.txt file an ending (_P1 or _P2 according to the if-condition). Right now I believe Matlab is going through the table and adding the ending P1 or P2 depending on which of the two if-conditions are met first.
Is there a way to collect all rows from the table which matches the conditions and give an ending according to the condition that matches with more rows than the other condition?
Thanks!
for x = 1 : size(Results,1)
if (Results(x,2) > limA2) && (Results(x,12) < limAR) && (Results(x,13) > limR2)
Type = [name + '_P1'];
elseif (Results(x,2) > limA1) && (Results(x,12) > limAR) && (Results(x,13) < limR1)
Type = [name + '_P2'];
end
end
##### 댓글 수: 1표시 없음숨기기 없음
Mathieu NOE 2022년 8월 23일
hello
do you have an issue with your code or what is the issue ?
do you have a working exmaple (with the txt file) to provide ?
tx

댓글을 달려면 로그인하십시오.

### 채택된 답변

Kartikay Sapra 2022년 8월 25일
편집: Kartikay Sapra 님. 2022년 8월 25일
Creating 2 variables which store the numbers of rows for each condition would help in finding the condition which makes up the majority:
count_1 = 0;
count_2 = 0;
for x = 1 : size(Results,1)
if (Results(x,2) > limA2) && (Results(x,12) < limAR) && (Results(x,13) > limR2)
count_1 = count_1+1;
elseif (Results(x,2) > limA1) && (Results(x,12) > limAR) && (Results(x,13) < limR1)
count_2 = count_2+1;
end
end
if (count1>count2)
% More rows satisfy the first condition
Type = [name + '_P1'];
elseif (count1<count2)
% More rows satisfy the second condition
Type = [name + '_P2'];
end
% Faster vectorised approach
col_2 = Results(:,2);
col_12 = Results(:,12);
col_13 = Results(:,13);
count1 = sum(any(Results((col_2 > limA2) & (col_12 < limAR) & (col_13 > limR2),:),2));
count2 = sum(any(Results((col_2 > limA1) & (col_12 > limAR) & (col_13 < limR1),:),2));
if (count1>count2)
% More rows satisfy the first condition
Type = [name + '_P1'];
elseif (count1<count2)
% More rows satisfy the second condition
Type = [name + '_P2'];
end
##### 댓글 수: 1표시 없음숨기기 없음
Tim K 2022년 8월 26일
Thank you a lot!

댓글을 달려면 로그인하십시오.

### 카테고리

Help CenterFile Exchange에서 Web Services에 대해 자세히 알아보기

R2021a

### Community Treasure Hunt

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

Start Hunting!