Can the matlab app designer support the timer?
Show older comments
t = timer;
t.StartDelay = 3;
t.TimerFcn = @(~, ~) eval('app.Label.Texy=datastr(now)');
start(t)
error: Try adding app to the static workspace.
How to solute this problem?
Answers (2)
denny
on 1 Sep 2017
2 Comments
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
on 31 Jul 2018
Perfect! That solves my problem too!
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);
-Cam
1 Comment
denny
on 1 Sep 2017
Categories
Find more on 启动和关闭 in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



