フィルターのクリア

Tic Toc seems disturb the operation of if-else function in the for-loop

2 ビュー (過去 30 日間)
M Min
M Min 2017 年 4 月 18 日
回答済み: M Min 2017 年 4 月 19 日
Hi,
I put the function tic and toc in the for loop but it looks that it disturb the working of if-else function in the same loop. Below is my code.
A = 10;
order = randperm(A);
for Trial = 1:A;
ImageOrder = [num2str(order(Trial)) '.jpg'];
IMG = imread(ImageOrder);
imshow(IMG);
Start = tic;
k = waitforbuttonpress;
Key(Trial) = double(get(gcf,'CurrentCharacter'));
if order(Trial) < 6;
if Key == 28
IMG_X = imread('x.jpg');
imshow(IMG_X);
pause(1);
end
else order(Trial) > 5;
if Key == 29
IMG_X = imread('x.jpg');
imshow(IMG_X);
pause(1);
end
end
Elapsed(Trial) = toc(Start);
end
Before I put Tic and Toc in the for-loop, if-else functions were working fine, but now they are not working. Is it correct that tic and toc disturb the operation of if-else? Thanks!
  1 件のコメント
Rik
Rik 2017 年 4 月 18 日
I don't any reason why this would be the case. Sometimes old variables can stick around longer than they should, which may cause unexpected results. You can avoid this by using functions instead of scripts, or (if you have a VERY good reason not to use functions) clear variables.
If you already this either of these, I don't have an explanation.

サインインしてコメントする。

回答 (2 件)

Jan
Jan 2017 年 4 月 18 日
編集済み: Jan 2017 年 4 月 18 日
No, tic/toc does not influence the if command. This is a magic idea and Matlab does not do such strange things. Otherwise you could not use Matlab for predicatble results.
The code contains another problem:
if order(Trial) < 6;
...
else order(Trial) > 5;
...
end
This is equivalent to:
...
else
order(Trial) > 5;
...
This compares the value of order(Trial) with 5, but ignores the result. Do you mean:
elseif order(Trial) > 5;
?
Seeing this problem, I'm convinced that tic/toc did not chnage anything. Try it by your own: comment the tic/toc lines and check, if this changes anything. Then fix the "elseif" problem and check it again.
  7 件のコメント
M Min
M Min 2017 年 4 月 19 日
Specifically, those conditions work only for its first iteration and from second iteration, there are problems.
M Min
M Min 2017 年 4 月 19 日
Oh I am really sorry but I missed to note about
order
I edit the post. Sorry again.

サインインしてコメントする。


M Min
M Min 2017 年 4 月 19 日
Finally I got what the problem is and it is not because of Tic and Toc as Rik, David, and Jan answered. I should have written
Key(Trial)
not
Key
Thank you for your answers. Appreciate!

カテゴリ

Help Center および File Exchange루프와 조건문 についてさらに検索

Community Treasure Hunt

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

Start Hunting!