How to wait for an event without stopping the program?

조회 수: 7 (최근 30일)
William Afonso
William Afonso 2015년 10월 21일
답변: William Afonso 2015년 11월 1일
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
Guillaume
Guillaume 2015년 10월 22일
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 2015년 10월 22일
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.

댓글을 달려면 로그인하십시오.

채택된 답변

Robert
Robert 2015년 10월 21일
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
dpb
dpb 2015년 10월 22일
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...

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

William Afonso
William Afonso 2015년 11월 1일
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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by