Can the matlab app designer support the timer?

10 views (last 30 days)
denny on 31 Aug 2017
Commented: Yao Zhang on 31 Jul 2018
t = timer;
t.StartDelay = 3;
t.TimerFcn = @(~, ~) eval('app.Label.Texy=datastr(now)');
error: Try adding app to the static workspace.
How to solute this problem?

Answers (2)

denny on 1 Sep 2017
I have done this job. Thanks to Cam Salzberger.
if true
properties (Access = public)
timer = timer('StartDelay', 4, 'Period', 0.5, 'TasksToExecute', Inf, 'ExecutionMode', 'fixedRate');
methods (Access = private)
% Code that executes after component creation
function startupFcn(app)
app.timer.TimerFcn = @(~, ~) myfunc2(app);
Then the m file.
function myfunc2(app) app.Label.Text=datestr(now);
Cam Salzberger
Cam Salzberger on 3 Sep 2017
Yep, that was what was going on in your last comment. The default for timers is to be a "single-shot", rather than executing at a fixed interval.
FYI, you can add the helper function to your app. There's a button in the upper left of the App Designer code view that says "Function" and has a drop-down. You can select to add the new function as a private or public method (you would only need private for this use-case).
Yao Zhang
Yao Zhang on 31 Jul 2018
Perfect! That solves my problem too!

Sign in to comment.

Cam Salzberger
Cam Salzberger on 31 Aug 2017
Hello Denny,
There are several issues going on here. First, a couple of typos. I believe you meant "Text" not "Texy" and "datestr" not "datastr".
Secondly, if you go to the link that is provided with the error message, you might notice this about "eval":
If possible, avoid using these functions altogether. See Alternatives to the eval Function.
What I believe is happening is that the timer function is called in its own workspace. In this case, you are providing it an anonymous function, so it is using the anonymous function's workspace. In this workspace, the variable "app" does not exist, so you wouldn't have access to the label anyway. Also, it seems that anonymous functions are given static workspaces, meaning you cannot assign new variables, which is what disallows it from creating a new struct called "app".
I would recommend making a new private function, called something like "setLabelToCurrentTime". Then you can just make the call to:
app.Label.Text = datestr(now);
within that function, and can just provide this to the timer object:
t.TimerFcn = @(~,~) setLabelToCurrentTime(app);
  1 Comment
denny on 1 Sep 2017
Firstly, Thank you very much.
But, the problem is still exist.
See the figure above, I want to show the time of "now" in the Label.
And I should added the following code at the startupFcn .
function startupFcn(app)
t = timer;
t.StartDelay = 3;
t.TimerFcn = @(~, ~) app.Label.Text=datestr(now);
It will error, because of the equal symbol in the code.
So, the equal symbol is forbidden in it. Then, I relized I can use the eval to avoid the equal symbol in the code.
t.TimerFcn = @(~, ~) eval('app.Label.Text=datestr(now)');
But, it error again, see the following figure.
It say that, the app should be added to the static workspace. So, I test your ideal, create a function to do this job. But, there is no place to added my function in the code Editor of the app designer.
So, I have to create a m file, as a function to do this job, and let the 'app' as the parameter of this function. So, the following code is added.
function startupFcn(app)
t = timer;
t.StartDelay = 0.3;
t.TimerFcn = @(~, ~) myfunc(app);
The following code is the content of the m file.
function myfunc(app)
But, it just worked at once, the string Label will not change again, it will not change at every 0.3 second.
and I test the following code with a global variable, it will not work as well.
function myfunc(app)
global xx
How can I do it?

Sign in to comment.


Find more on 使用 App 设计工具开发 App in Help Center and File Exchange


Community Treasure Hunt

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

Start Hunting!