Matlab GUIDE - Change color while loop is running?

5 Ansichten (letzte 30 Tage)
thebasher
thebasher am 27 Mär. 2014
Kommentiert: Image Analyst am 27 Mär. 2014
Hi,
I'm trying to simulate a low battery scenario in matlab using GUIDE. I was wondering how to get the GUIDE to pop up with a green box (representing an LED), run a loop, then the "LED" turns red once the loop gets to a certain point.
I'm able to do this once I press a pushbutton, but how can I simulate this without pressing the button? Here's what I have:
if true
% --- Executes on button press in pushbutton12.
function pushbutton12_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton12 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
battery=50
for time=1:100
while battery>0
battery=battery-2
if battery<20 && battery>0
set(hObject,'BackgroundColor','red')
set(handles.Screen,'String','Low Battery. Plug in Charger')
else
end
pause(0.1)
end
end
end
Any ideas on how I can do this?
Thanks for your time.

Akzeptierte Antwort

Joseph Cheng
Joseph Cheng am 27 Mär. 2014
Bearbeitet: Joseph Cheng am 27 Mär. 2014
So.... you can use timer which if used properly you can accomplish what you want.
Code to get you started %i inserted this into the openfnc portion
tic;
handles.timer = timer(...
'ExecutionMode', 'fixedRate', ... % Run timer repeatedly
'Period', 1, ... % Initial period is 1 sec.
'TimerFcn', {@update_display,handles}); % Specify callback function
% Update handles structure
start(handles.timer);
my update_display looks like this
function update_display(hObject,eventdata,handles)
elapsetime = toc;
if round(elapsetime) ==10
set(handles.pushbutton1,'BackgroundColor','y')
elseif round(elapsetime) ==20
set(handles.pushbutton1,'BackgroundColor','r')
end
10 and 20 are in seconds... with this you actually use your pushbutton12 that you've already wrote... I think... I'll leave it to you to figure out how to stop and reset the timer.
if you want a better example of timer you can go see refresh gui data display
  1 Kommentar
thebasher
thebasher am 27 Mär. 2014
Much appreciated, I would have never considered the timer...Thanks so much for your time and help.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Image Analyst
Image Analyst am 27 Mär. 2014
Well, how would you like to start the simulation running?
  2 Kommentare
thebasher
thebasher am 27 Mär. 2014
When the GUI pops up, right after I hit run. I want to loop to start "draining" the battery. So at first, the LED is green, then after a few seconds it'll turn red.
The code I put above does it at the press of a button, but I'd like it to run automatically.
I hope I made sense.
Image Analyst
Image Analyst am 27 Mär. 2014
You can put the code in the OpeningFcn to start it running as soon as the GUI is launched. You don't need a timer, unless you want to. You could do it like you had it, with pause(). Though a timer frees up your GUI to do other things if you want other things to happen while your simulation is draining the battery.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Programming 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