Clear Filters
Clear Filters

Matlab only runs a line is I pause before or in debug mode.

3 views (last 30 days)
I have a GUI with a listbox and a UITable.
The UITable has a cell selection callback. This callback changes the Value (selected items) of the listbox. This happens so that when I click an item in the UITable, that item is also selected in the Listbox.
I also have a button in my GUI with a Callback that runs a function which has the handles of the GUI as an input:
my_function(handles, someOtherData)
Inside my_function I update the data of the UITable, which makes the selection to become empty, and therefore the selection of the listbox to become empty. So, before this happens, I save my selected items into saved_selection.
However, after updating the UITable, I call
set(handles.my_listbox, 'Value', saved_selection)
but the selection in the Listbox is not set - it remains empty. The strange thing is that it works fine in debug mode (i.e. setting a break point somewhere and run line by line, everything works as it should, and the listbox selection is updated correctly). Also, if I add a pause(0.1) before the "set", it also works fine. But, if I use a pause(0.000001) it does not work. Thus, it seems like the set function is being run before it should.
I'm not sure what is causing this. Does Matlab run functions parallelly? If so, can it be that when I update the data in the UITable, that this process is running in parallel with the rest of my_function, and then the "set" is called before the update UITable is completed?
How can prevent this?

Accepted Answer

Sean de Wolski
Sean de Wolski on 9 Apr 2018
Edited: Sean de Wolski on 9 Apr 2018
Put a drawnow on the preceding line which will forcefully flush the event queue. Debug, pause, do this implicitly.
Also, consider using the much more modern app designer for your gui development. There is now a guide to app designer conversion tool if you have existing user interfaces to migrate.
doc appdesigner
  2 Comments
riverCN
riverCN on 9 Apr 2018
Edited: riverCN on 9 Apr 2018
Thanks Sean. This solved the issue indeed. Would you mind to explain why this happens so that I can avoid stumbling into these problems in the future?
Also, thanks a lot for the suggestion of the appdesigner. I was completely unaware of the existence of this tool. Seems so much better. Since when is it available?
Sean de Wolski
Sean de Wolski on 10 Apr 2018
Edited: Sean de Wolski on 10 Apr 2018
You're welcome!
for ii = 1:100
plot(1:ii)
end
If you run this, you'll only see the final plot. MATLAB has offloaded 100 graphics events to an event queue but then keeps running and doesn't waste any time flushing those events. This is a feature most of the time. However, if you need access to those immediate events for the purpose of animation or user experience, you can force it with drawnow or pause(0.0001).
for ii = 1:100
plot(1:ii)
drawnow
end
Now you'll see all 100 iterations but it will take longer to run.
In apps, I tend to use drawnow fairly liberally when manipulating any graphics object.
App designer was first released in 16a and has been growing rapidly with every release since.

Sign in to comment.

More Answers (0)

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!