Update textarea when printing to the console

조회 수: 5 (최근 30일)
John F
John F 2022년 4월 19일
편집: John F 2022년 4월 19일
I have an app where I have setup a diary to store the outputs to the console to a file. In this app, I have a text area that I would like to update its Value whenever something is printed to the console. For example, this app uses a Simulink model that outputs diagnostics to the console if something went wrong.
How could I go about this? Is there perhaps any event listener that I could use?
app.dfile = 'app_log.txt';
if exist(app.dfile, 'file')
delete(app.dfile);
end
diary(app.dfile)
diary on
cons_tab = uitab(PlotTabGroup,'Title','Console'); % PlotTabGroup is a TabGroup with multiple tabs
cons_txt = uitextarea(cons_tab,'Value',fileread(app.dfile),...
'Position',[0 0 cons_tab.Position(3) cons_tab.Position(4)],...
'Tag','console');
  댓글 수: 2
Eric Delgado
Eric Delgado 2022년 4월 19일
Hi @John F, you could create a timer object in the startup of your app. See atached!
Hope it helps! :)
% Startup
function startupFcn(app)
diary('app_log.txt')
diary on
app.tmr = timer("ExecutionMode", "fixedDelay", ...
"Period", 10, ...
"TimerFcn", @(~,~)timerCallback(app));
start(app.tmr)
end
% Callback
function timerCallback(app)
try
app.Log.Value = fileread('app_log.txt');
catch ME
uialert(app.UIFigure, ME.message, 'Error', 'Icon', 'error')
end
end
John F
John F 2022년 4월 19일
편집: John F 2022년 4월 19일
@Eric Delgado This works but if someone finds something that doesn't involve a timer that would be better. Also, if someone uses this in the future, you also need to remove the timer at the end or it will remain even if the app is closed:
stop(app.tmr)
delete(app.tmr)
Thank you for the suggestion though!

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by