How to have a function constantly execute in a Matlab Guide GUI?

12 vues (au cours des 30 derniers jours)
HelloWorld
HelloWorld le 8 Fév 2017
Modifié(e) : Konvictus177 le 10 Nov 2022
I am trying to create the following GUI:
Monitor a textbox. If the input of the textbox changes, change the background color of another textbox for half a second, and then revert it back.
The way I thought about doing this is having a function that is constantly executing, checking in a while loop if the input of the textbox has changed. If it has changed, change the color of the textbox and wait half a second. Then change it back.
The problem is that I am not sure how to have a function that is always executing. All of the button functions and other things in the matlab gui guide only execute when they are pushed or pressed.

Réponses (1)

Randy Acheson
Randy Acheson le 10 Fév 2017
Modifié(e) : Randy Acheson le 10 Fév 2017
You can have a callback called regularly during the duration of a program by using the 'timer' object. Create a timer in the OpeningFcn of your GUIDE application, and assign it the callback you wish to call repeatedly. Here is an example:
function TestGUI_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to untitled (see VARARGIN)
% Choose default command line output for untitled
handles.output = hObject;
t = timer();
t.Period = 2;
t.ExecutionMode = 'fixedRate';
t.TimerFcn = @TimerFcn; % Here is where you assign the callback function
handles.timer = t; % Put the timer object inside handles so that you can stop it later
start
(t)
% Update handles structure
guidata(hObject, handles);
Then, when you want to stop the timer, you can do so in any callback in your application with this line:
stop(handles.timer)
For an alternative approach, see this MATLAB Answers post:
  1 commentaire
Konvictus177
Konvictus177 le 3 Nov 2022
Modifié(e) : Konvictus177 le 10 Nov 2022
Does this slow down the app? Does calling the function repeatedly with the timer object slow down other processes running in the app?

Connectez-vous pour commenter.

Catégories

En savoir plus sur Environment and Settings 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