imrect in infinite loop

11 ビュー (過去 30 日間)
jojo9540
jojo9540 2017 年 3 月 29 日
編集済み: jojo9540 2017 年 3 月 29 日
I have a Matlab UI where I want the user to input several areas using imrect as soon as a radiobutton is selected. It is unknown how many areas will be selected so the selection needs to be in an infinite loop.
As soon as another radiobutton is selected, the imrect input should stop, which I cannot get to work.
Here is a minimal working example:
function mwe
ax = axes('Position', [0 0 1 1]);
bg = uibuttongroup('Position',[0 0 .15 1], 'SelectionChangedFcn',{@bselection, ax});
r1 = uicontrol(bg, 'Style','radiobutton', 'String','Option 1', 'Position',[10 250 100 30]);
r2 = uicontrol(bg, 'Style','radiobutton', 'String','Option 2', 'Position',[10 225 100 30], 'Value',1);
function bselection(source, event, ax)
switch event.NewValue.String
case 'Option 1'
while true
h = imrect(ax);
% do stuff
delete(h);
end
case 'Option 2'
% do not show imrect and do other stuff
end
I appreciate any help.
  1 件のコメント
Adam
Adam 2017 年 3 月 29 日
Can you not just test the state of the radiobutton within your while loop rather than use the callback to deal with 'Option 2'?

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

採用された回答

Jan
Jan 2017 年 3 月 29 日
switch event.NewValue.String
case 'Option 1'
set(ax, 'UserData', 1);
while get(ax, 'UserData') == 1
h = imrect(ax);
% do stuff
delete(h);
end
case 'Option 2'
set(ax, 'UserData', 0);
end
Do you want to cancel the currently active input of the rectangle? Does the original code contain a pos = wait(h) ?
  1 件のコメント
jojo9540
jojo9540 2017 年 3 月 29 日
編集済み: jojo9540 2017 年 3 月 29 日
Thank you very much for your help! Your solutions works quite well.
I found a short way to check the radiobutton selection by replacing while true with
while strcmp(source.SelectedObject.String, 'Option 1')
The only problem though is that after changing the radiobutton selection, imrect will still be executed one last time. To solve this I use a workaround by simulating an escape button press when Option 2 is selected, so in case 'Option 2' I have added
robot = java.awt.Robot;
robot.keyPress(java.awt.event.KeyEvent.VK_ESCAPE);
robot.keyRelease(java.awt.event.KeyEvent.VK_ESCAPE);
Full code:
function mwe
ax = axes('Position', [0 0 1 1]);
bg = uibuttongroup('Position',[0 0 .15 1], 'SelectionChangedFcn',{@bselection, ax});
r1 = uicontrol(bg, 'Style','radiobutton', 'String','Option 1', 'Position',[10 250 100 30]);
r2 = uicontrol(bg, 'Style','radiobutton', 'String','Option 2', 'Position',[10 225 100 30], 'Value',1);
function bselection(source, event, ax)
switch event.NewValue.String
case 'Option 1'
while strcmp(source.SelectedObject.String, 'Option 1')
h = imrect(ax);
% do stuff
delete(h);
end
case 'Option 2'
robot = java.awt.Robot;
robot.keyPress(java.awt.event.KeyEvent.VK_ESCAPE);
robot.keyRelease(java.awt.event.KeyEvent.VK_ESCAPE);
% do other stuff
end

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMigrate GUIDE Apps についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by