Why is my code not working?

I have the following code that allows me to increase/decrease the value of d by pressing the arrow keys.
d = 20
f = figure;
k=1;
while k
waitforbuttonpress;
if get(gcf,'CurrentCharacter')==28 %for left arrow key
d=d-10
elseif get(gcf,'CurrentCharacter')==29 %for right arrow key
d=d+10
elseif get(gcf,'CurrentCharacter')==32 %for space bar
break
end
end
It almost works, except I would like the loop to end or "break" when I press the space bar. For some reason when I try pressing the spacebar MatLab returns with "There is no CurrentCharacter property on the Figure class". I tried a slightly different code by changing "break" in the 11th line to "k=0" but I got the exact same error.
Thank you!!!

3 件のコメント

David Goodmanson
David Goodmanson 2016 年 10 月 21 日
編集済み: David Goodmanson 2016 年 10 月 21 日
Hello Emily, the code above seems to work. Is is possible that you misspelled 'CurrentCharacter' on the space bar line for the code that didn't work?
Chaya N
Chaya N 2016 年 10 月 21 日
Emily, the code works just fine. Also you need not explicitly declare f = figure; on line 2. The waitforbuttonpress call will do that automatically.
Emily Smith
Emily Smith 2016 年 10 月 21 日
Oops, yeah I misspelled it. Thanks!

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

回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeProgramming についてさらに検索

質問済み:

2016 年 10 月 21 日

コメント済み:

2016 年 10 月 21 日

Community Treasure Hunt

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

Start Hunting!

Translated by