Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

Print message about which loop has been entered, after finishing for loop

조회 수: 1 (최근 30일)
lightroman
lightroman 2017년 11월 21일
마감: MATLAB Answer Bot 2021년 8월 20일
I have 4 if statements in a for loop. I need to know which if statement was entered. Can be just one or all 4. after the for loop finishes I will need to print a message saying which entered without repeats or if none were. each if statement being A B C or D in the print message.
I tried assigning a letter to each if statement and if that was entered then add to an array. then look through the array after the for loop and print a message after going through 4 more if statements. I am over complicating it
ex: A and D were entered. None were entered. etc.
let me know if not clear i am ESL. Doing project for fun.

답변 (1개)

KL
KL 2017년 11월 21일
set counters and use it with disp
counterA = 0;
counterB = 0;
counterNone = 0;
for k=1:10
%code
if (condition A here)
counterA = counterA+1;
disp(['condition A is true. counterA = ' num2str(counterA)]);
elseif (condition B here)
counterB = counterB+1;
disp(['condition B is true. counterB = ' num2str(counterB]);
else
counterNone = counterNone+1;
disp(['No condition is true. counterNone = ' num2str(counterNone]);
end
end
  댓글 수: 2
lightroman
lightroman 2017년 11월 21일
편집: lightroman 2017년 11월 21일
hi thank you. I need to only print after all iterations run. meaning after for loop or after last iteration. so somehow indicate which were entered. and only print after all 10 iterations have occured. because i only care if a if statement was enter least once.
KL
KL 2017년 11월 21일
편집: KL 2017년 11월 21일
simply move all the disp statements outside the loop and customize your message. For example,
disp(['condition A was true ' num2str(counterA) ' times'])

이 질문은 마감되었습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by