필터 지우기
필터 지우기

How to write if condition for a column in a .txt file

조회 수: 1 (최근 30일)
Mech Stud
Mech Stud 2018년 3월 22일
댓글: Greg 2018년 3월 22일
I have a variable C that generates the last column of a .txt file. I want to only save the data as a .txt file if the C that is being generated is 1. But it is not working with if condition. However without if condition it prints the complete data perfectly.
If (C==1);
result = [A B C];
fid = fopen('XYZ.txt','wt');
fprintf(fid,'Start\n');
for ii = 1:size(result,1)
fprintf(fid,'%g\t',result(ii,:));
fprintf(fid,'\n');
end
fprintf(fid,'End\n');
fclose(fid)
end
  댓글 수: 2
Greg
Greg 2018년 3월 22일
Did you use a breakpoint to check the value of C, then step to see if it enters the if block? Occasionally, == fails for integers due to floating point round-off.
KSSV
KSSV 2018년 3월 22일
What are the possible values of C ?

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

채택된 답변

Greg
Greg 2018년 3월 22일
I think I understand now - you want to downselect each row based on its value of C. We were looking at C as a scalar (clearly not the case now that I look again, you concatenate C with A and B). Do a search for one of the many questions about non-scalar if conditions - they just don't work.
If you only want to print rows of the matrix where column 3 == 1:
blnKeep = logical(C);
result = result(blnKeep,:);
%%%Your working fprintf code here, without the if condition
Then you should probably update your question to call that out more explicitly.
  댓글 수: 2
Mech Stud
Mech Stud 2018년 3월 22일
Thanks. It was helpful.
Greg
Greg 2018년 3월 22일
Happy to help.
Please accept this answer if it is accurate and complete.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by