フィルターのクリア

How to change pushbutton colour in Guide without pressing it ?

1 回表示 (過去 30 日間)
Karthik Muthineni
Karthik Muthineni 2018 年 2 月 11 日
コメント済み: Saad Oussaada 2020 年 3 月 8 日
I have a GUI that reads the DHT11 sensor data attached to Arduino. I also have pushbutton in my GUI. Now, how do I change pushbutton colour based on incoming sensor data. For, example if the temperature is below 30 celsius then pushbutton should turn to Green otherwise it should turn to Red.
  6 件のコメント
Karthik Muthineni
Karthik Muthineni 2020 年 3 月 7 日
Hello Saad Oussaada,
You can create GUI in MATLAB. For that type guide command in command editor, and a window will pop-up. From there you can follow this link to create GUI for your case. You can drag and drop pushbuttons, textarea, axes, and many more widgets. Once you select particular widget you need to assign code for it to do particular task. In your case, if you want to read DHT data and display it on graph you can use this piece of code.
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
delete(instrfind({'Port'},{'COM6'})); % Serial port of your dht microcontroller
s = serial('COM6');
time=1000;
i=1;
while(i<time)
i=i+1;
fopen(s)
fprintf(s, 'Your serial data goes here')
out = fscanf(s)
Temp(i)=str2num(out(1:2));
disp(Temp)
fclose(s)
plot(handles.axes1,Temp)
xlabel('Time (seconds)');
ylabel('Temperature (°C)');
set(handles.text21,'String',num2str(Temp)); % Displays Temperature value in text widget of id 21
drawnow;
end
delete(s)
clear s
if Temp <= 35
set(hObject,'BackgroundColor','green')
else
set(hObject,'BackgroundColor','red')
end
guidata(hObject,handles);
Saad Oussaada
Saad Oussaada 2020 年 3 月 8 日
hello Karthik Muthineni
thank you so mush

サインインしてコメントする。

採用された回答

Karthik Muthineni
Karthik Muthineni 2018 年 2 月 11 日
Thank you, Walter, its working.

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2018 年 2 月 11 日
You can set the BackgroundColor property of the uicontrol handle
handles.stop_button.BackgroundColor = [0.8 0 0] %red

カテゴリ

Help Center および File ExchangeMigrate GUIDE Apps についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by