https://kr.mathworks.com/help/matlab/creating_guis/wind-speed-gui-in-app-designer.html
핵심코드
1) properties (Access = Private)
properties (Access = private) WindSpeedTimer end |
2) methods (Access = Private)
function WindSpeedTimerFcn(app, ~, ~) tnow = datetime("now", "TimeZone", "Asia/Seoul"); weatherdata = webread("https://api.thingspeak.com/channels/12397/feeds/last.json"); y = str2double(weatherdata.field2); xLL = app.UIAxes.XLim(1); xUL = app.UIAxes.XLim(2); if (tnow > xUL) xtra = seconds(15); app.UIAxes.XLim = [xLL+xtra tnow+xtra]; app.UIAxes.XTick = linspace(xLL+xtra, tnow+xtra, 5); end plot(app.UIAxes, tnow, y, "ob"); yLL = max(y-10, 0); yUL = y + 10; app.UIAxes.YLim = [yLL yUL]; app.UIAxes.YTick = round(linspace(yLL, yUL, 5), 2, "significant"); end |
3) Callback Function
3-1) 앱이 시작될 때 데이터 로딩 및 초기화를 수행하는 기능
function startupFcn(app) plot(app.UIAxes, datetime("now","TimeZone","Asia/Seoul","Format","hh:mm:ss"),50,"MarkerEdgeColor","none"); t = datetime("now","TimeZone","Asia/Seoul","Format","hh:mm:ss") + seconds(1:60); xLL = t(1) - seconds(5); xUL = t(length(t)); app.UIAxes.XLim = [xLL xUL]; app.UIAxes.XAxis.TickLabelFormat = "hh:mm:ss"; app.UIAxes.XTick = linspace(xLL, xUL, 5); app.UIAxes.YLim = [0 50]; app.UIAxes.YTick = round(linspace(0,50,5), 2, "significant"); grid(app.UIAxes, "on"); hold(app.UIAxes, "on"); app.WindSpeedTimer = timer("ExecutionMode","fixedRate","Period",5,"BusyMode","queue","TimerFcn",@app.WindSpeedTimerFcn); end |
3-2) Start 버튼을 클릭할 때 기능하는 콜백함수
function StartButtonPushed(app, event) if strcmp(app.WindSpeedTimer.Running, "off") start(app.WindSpeedTimer); end end |
3-3) Stop 버튼을 클릭할 때 기능하는 콜백함수
function StopButtonPushed(app, event) stop(app.WindSpeedTimer); end |
3-4) 창을 닫을 때 기능하는 콜백함수
function UIFigureCloseRequest(app, event) stop(app.WindSpeedTimer); delete(app.WindSpeedTimer); delete(app); end |