Waiting for a option to be selected in a listbox in Matlab GUI before executing remaining m file.

6 vues (au cours des 30 derniers jours)
Hi,
i have a matlab script that calls a GUI with a listbox. Lets call the script Part1.m and, and the Gui Part2.fig (with the associated function Part2.m)
I would like the main script (Part1.m) to wait until a option from the listbox is selected before execution.
As an example, support the data in Part1.m is:
handle_to_part2 = Part2();
handles_part2 = guidata(handle_to_part2);
aPlot=axes;
plotPointX = 10;
plotPointY = 20;
% fig
scatPlot1 = scatter(aPlot, plotPointX, plotPointY,50,'filled');
set(scatPlot1, 'XDataSource', 'plotPointX');
set(scatPlot1, 'YDataSource', 'plotPointY');
set(aPlot, 'XLim', [-100 100]);
set(aPlot, 'YLim', [-100 100]);
mul=1;
%{
here instead of setting the 'mul' value, I would like to wait for the mul value to come from the list box
so if the list box label is gListBox
and its values are
1
2
-5
-8
the value of 'mul' should only be taken from this. Therefore the program must wait until this is selected. Then continue once done.
%}
for n=1:10
var=n*mul;
set(handles_part2.text2, 'String', num2str(var) );
drawnow;
pause(0.1);
plotPointX=var;
plotPointY=n;
refreshdata(scatPlot1, 'caller');
drawnow;
end
So, here as explained in the comment, the value of 'mul' should be set in the list box and the program should wait until it is done. im open to adding a continue button as well, but once again, this would mean it has to wait until both the option from the list box is selected and continue is pressed.
Thanks in advance for the help,
Regards,
Craig

Réponses (2)

Jan
Jan le 14 Nov 2016
The direct way would be to include the code in the callback of the listbox. Then selecting a new value triggers the execution of the code automatically. This is cleaner then letting any other code waiting around. If you e.g. close the figure manually, before the value is selected, the behavior is not well defined anymore.
  2 commentaires
Craig Kavan Pinto
Craig Kavan Pinto le 15 Nov 2016
That could work, but the gui is only meant for presentation purposes. I have, as of now, used the 2 part method as I need to update the gui quite often, and since matlab automatically creates the gui function, it seems to keep erasing my old code in the gui everytime i add a new item.
Jan
Jan le 21 Nov 2016
@Craig: You do not have to insert the code in the GUI's M-file. You are right, that keeping the code for computations separately is a good programming practize. So you can add just one line to call the external computation in the GUI callback. The main idea is not to "remote control" the GUI, but let the GUI perform everything related to user interactions.

Connectez-vous pour commenter.


Walter Roberson
Walter Roberson le 19 Nov 2016
If you need to wait for a value to be set, you should use waitfor(). But keep in mind that if you wait for a change in a property, that you will keep waiting for-ever if the user selects the entry that is already selected.
The currently-selected item of a listbox is determined by its Value property, and you need to have set (or allowed to default) the Value property to be from 1 to the number of items in the listbox, with setting it to 0 or outside the range not being permitted. Exception: you can set the Value property to [] (empty) to have nothing selected. Remember to set it back to [] when you want to permit a re-trigger waitfor() based upon a change of value.
You can also work this by using waitfor() or uiwait() without specifying a particular property, in which case you would have the Callback for the listbox execute a uiresume()

Catégories

En savoir plus sur Interactive Control and Callbacks dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by