Clear Filters
Clear Filters

how to Automate a button click after X-Seconds to update values

2 views (last 30 days)
Hi everyone,
I have an issue with MATLAB GUIs that I don't know how to solve. i want to automate the "Push Button" so that after x-Seconds the values off my Variables(Distance traveled, Required_Energy ...) be updated automatically. (The variables are available as Vector in workspace)
basically I want to run the Butto after x-Second automatically without have to click on it.
I was thinking about a Solution with timer but i dont know how to do it.
i will apreciate any Help many thanks Jay

Accepted Answer

Geoff Hayes
Geoff Hayes on 25 Aug 2016
Jay - are these variables in your workspace because you are using Simulink? If so, you could create a timer object in the OpeningFcn of your GUI as
handles.timer = timer('Name','MyTimer', ...
'Period',5, ...
'StartDelay',1, ...
'TasksToExecute',inf, ...
'ExecutionMode','fixedSpacing', ...
'TimerFcn',{@timerCallback,handles.figure1});
guidata(hObject,handles);
start(handles.timer);
In the above, we create the timer that will fire every five seconds. We assign a callback, timerCallback, and pass an additional parameter which the handle to the GUI/figure. We then save the updated handles structure and start the timer.
Your callback would then be
function [] = timerCallback(~,~,guiHandle)
if ~isempty(guiHandle)
% get the handles
handles = guihandles(guiHandle);
% collect your data from the workspace
% update the text fields of your GUI using *handles*
% an example:
% find the distance traveled
distTraveled = randi(255,1,1);
% update the distance traveled text field
set(handles.textDistanceTraveled,'String',num2str(distTraveled));
end
You would probably need to use evalin to get your data from the base workspace. textDistanceTraveled is the name/tag for your distance traveled text field. (I suspect that it is quite different in your code.)
Try the above and see what happens! (See also https://www.mathworks.com/matlabcentral/answers/178682-collecting-a-variable-every-two-minutes for a similar problem.)
  1 Comment
Jay Samin
Jay Samin on 26 Aug 2016
Hi Geoff,
thank u for the Quick answer :) yes, im getting the variables from Simulink. i will try it and let u know.
thnx again

Sign in to comment.

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Tags

No tags entered yet.

Community Treasure Hunt

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

Start Hunting!