Lines of code (in app) executed only with breakpoints

3 ビュー (過去 30 日間)
Roberto Tumolo
Roberto Tumolo 2022 年 11 月 24 日
コメント済み: Roberto Tumolo 2022 年 12 月 8 日
Hello all,
I created a very trivial app (that I'm attaching) that when clicking on tab2 it should return to tab1 automatically. So basically the purpose is to avoid accessing tab2. However, line 18 works only if a breakpoint is inserted, not otherwise. Very weird to me :-D
Thank you!

回答 (1 件)

Yash Srivastava
Yash Srivastava 2022 年 12 月 6 日
The reason why this happens is because of the order of events in this workflow, and how the breakpoint disrupts them. The typical event order goes like this:
<user mousedown>
-> ButtonDownFcn (Tab/any other component with a ButtonDownFcn)
<user mouseup>
-> SelectionChangedFcn (TabGroup)
-> WindowButtonUpFcn (Figure)
When a breakpoint is set in the 'ButtonDownFcn', it stalls the processing of the 'ButtonDownFcn', which allows the interactive tab selection to Tab 2 to go through. Then, when you let the code continue from the breakpoint, the 'ButtonDownFcn' finishes processing, which then programmatically sets the Tab to Tab 1.
Uninterrupted, the ButtonDownFcn is processed first (to set the SelectedTab to Tab 1), and then the selection is changed to Tab 2 because the selection event happens after the mousedown (i.e. during the mouse up). So the app ends up on Tab 2.
As a workaround for this, instead of setting the 'SelectedTab' in the 'ButtonDownFcn', the you can use the "TabGroup's" "SelectionChangedFcn" to change the "SelectedTab". That will ensure that it is processing the event after the Tab is selected.
The callback would look something like this:
% Selection change function: TabGroup
function TabGroupSelectionChanged(app, event)
selectedTab = app.TabGroup.SelectedTab;
if (selectedTab == app.Tab2)
app.TabGroup.SelectedTab=app.Tab;
end
end
  1 件のコメント
Roberto Tumolo
Roberto Tumolo 2022 年 12 月 8 日
HI Yash,
thanks a lot for your in depth answer and workaround (which clearly works)!

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

カテゴリ

Find more on Interactive Control and Callbacks in Help Center and File Exchange

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by