Matlab only runs a line is I pause before or in debug mode.
3 次查看(过去 30 天)
显示 更早的评论
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?
0 个评论
采纳的回答
Sean de Wolski
2018-4-9
编辑:Sean de Wolski
2018-4-9
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 个评论
Sean de Wolski
2018-4-10
编辑:Sean de Wolski
2018-4-10
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Migrate GUIDE Apps 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!