Assigning tstart to tic, when tic is a timer start function

Hello, Im using a timer object to set some function going with a delay that occurs at a time whilst a loop containing a pause is executed.
app.MessagesTextArea.Value='';
ReportMessage(app,'Starting Timer, msg in 5s');
% Can use an anonymous function (for the "TimerFcn" callback) that take two input arguments (for "obj" and "event")
% and calls the other function with its corresponding input argument ('msg')
t = timer('TimerFcn',@(~,~)app.ReportMessage('...Timermsg'),...
'StartDelay',5);
% Execute the Timer
start(t);
tstart=tic;
for i=1:20
ReportMessage(app,num2str(toc(tstart)));
pause(0.5)
end
where my function ReportMessage is below.
function ReportMessage(app,msg)
currString=get(app.MessagesTextArea,'Value');
%currString=[{char(msg)};currString]; %add to top of message box
currString=[currString; {char(msg)}]; %add to bottom of message box
app.MessagesTextArea.Value=currString;
drawnow;
scroll(app.MessagesTextArea,'bottom');
end
This works fine, however, sometimes the function I call has a tic in, so this is why I assign tstart to this tic.
Now I realised I can also setup a timerstart function, so i thought it would be better to put the tic inside this to make the start(t) and tic more coincindent in time.
t.StartFcn = @(~,~)tic
This works, but how would I assing tstart to this tic?
Thanks
Jason

5 件のコメント

Stephen23
Stephen23 2025 年 5 月 16 日
"This works, but how would I assing tstart to this tic?"
Callback functions are not called with an output argument, so with that function... you can't.
Define a normal function, call TIC, obtain and store its output. Specify that function as the STARTFCN.
Jason
Jason 2025 年 5 月 16 日
Thanks Stephen.
Not sure whare I've gone wrong:
function tstart=TIC(app)
tstart=tic;
end
t.StartFcn = @(~,~)app.TIC;
Unrecognized function or variable 'tstart'.
Jason
Jason 2025 年 5 月 16 日
編集済み: Jason 2025 年 5 月 16 日
Ah, I haven't stored tstart anywhere!!
When I do this:
properties (Access = public)
myStart % To use with tic
end
I can then call it from
app.myStart
This is my full code:
% You can use an anonymous function (for the "TimerFcn" callback) that take two input arguments (for "obj" and "event")
% and calls your other function with its corresponding input argument ('msg')
t = timer('TimerFcn',@(~,~)app.ReportMessage('...Timermsg'),...
'StartDelay',5);
t.StartFcn = @(~,~)app.TIC;
% Execute the Timer
start(t);
% tstart=tic;
for i=1:20
ReportMessage(app,num2str(toc(app.myStart)));
pause(0.48)
end
and it all works. Is this the best/only way to achieve this?
Stephen23
Stephen23 2025 年 5 月 16 日
"Is this the best way to achieve this?"
Defining the value as an app property seems a like good approach.
Jason
Jason 2025 年 5 月 16 日
Thanks

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

 採用された回答

Benjamin Kraus
Benjamin Kraus 2025 年 6 月 9 日
編集済み: Benjamin Kraus 2025 年 6 月 9 日

0 投票

As @Stephen23 said, if you have an app, storing the output from tic in a property on your app is a good approach. However, another option would be to store the output from tic in the UserData property on the timer object. The UserData property is designed to store arbitrary data associated with the timer, and the start time seems like a good use for that property.
t = timer('TimerFcn',@(t,~) disp(toc(t.UserData)),...
'StartDelay',5,'StartFcn',@(t,~) set(t,'UserData', tic));
start(t)

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeStartup and Shutdown についてさらに検索

製品

リリース

R2024b

タグ

質問済み:

2025 年 5 月 16 日

編集済み:

2025 年 6 月 9 日

Community Treasure Hunt

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

Start Hunting!

Translated by