How to wait for an event without stopping the program?

26 Ansichten (letzte 30 Tage)
William Afonso
William Afonso am 21 Okt. 2015
Beantwortet: William Afonso am 1 Nov. 2015
Hello world,
I am using a GUI which displays a list of names. When the user selects a name among this list, I want to call a function, ONLY if the name is selected more than x seconds. I don't want to use a while loop to wait the x seconds, because I don't want my program to cumulate while loops each time the user selects a name. Does anyone know how to solve this problem? Is it possible to create this kind of event/listeners in Matlab? If it is possible, how to do it? Thanks for your help
  5 Kommentare
Guillaume
Guillaume am 22 Okt. 2015
Any user interface that does not behave the same as is standard for the platform is very likely to result in user frustration. Particularly, if there is no cue to the new behaviour.
The expected behaviour of a user interface is that clicking on something has an immediate effect. Not after you press on it for a number of seconds. You're making it harder to use your interface. You may find that instead of trying to adjust to your special interface, the user is simply not going to use it.
What is the rationale for forcing the user to click on the list item for a minimum amount of time? And how are you planning to emulate that for keyboard input?
Walter Roberson
Walter Roberson am 22 Okt. 2015
I could imagine that if you are using up/down to move between list selections then you would not want the selection chosen as soon as you got there because you might want to move further on (or back). I can imagine someone thinking that a good approach would be to allow this kind of movement and to consider a location selected if the user "stopped moving" the keys for long enough. However, the built-in interface for such things depends not upon timing but upon the user pressing return / enter when the item to be selected is reached.
William, pushbuttons do not have a "release" callback: they only fire when the item is clicked on.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Robert
Robert am 21 Okt. 2015
You could accomplish this with a timer object. When a name is selected, use the callback to create and start a timer with ExecutionMode set to SingleShot (try doc timer for details). Then after the StartDelay of the timer has elapsed, the timer's TimerFcn will execute.
If the user selects a new name before the time is up, have the selection callback stop the timer. If it is possible to deselect a name without triggering that callback, have the timer function check to be sure the original name is still selected before executing the rest of your code.
  1 Kommentar
dpb
dpb am 22 Okt. 2015
And the latter event described above would be one of those most surprising and puzzling occurrences w/ such an interface for the user--once had gotten used to the idea of having to hold a while--the hold wouldn't have satisfied on the deselected file but the second would be selected without there being the requisite time delay, hence possibly (probably?) getting the wrong one. In the extreme, if the timer were almost ready to time out when the deselection took place, it would be only an instant on the alternate and BOOM! -- gotcha'!
Just to illustrate to OP it's not a good idea of a user interface...

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

William Afonso
William Afonso am 1 Nov. 2015
I'm a bit late, but thanks Robert! As you suggested, I used:
  • a timer defined in the openingFcn of my GUI:
global t;
t = timer;
t.TimerFcn = @(x,y)myfunction;
t.StartDelay = 0.3; % delay to wait before calling myfunction
t.UserData = handles; % handles are the parameters to pass to myfunction
  • when the user selects a name among the list, it generates a callback to a function which does:
global t;
stop(t);
start(t);
  • myfunction :
function myfunction(timer,~)
handles = get(timer,'UserData'); % get the necessary parameters to myfunction
...
end

Kategorien

Mehr zu Interactive Control and Callbacks finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by