Can the matlab app designer support the timer?
古いコメントを表示
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?
回答 (2 件)
denny
2017 年 9 月 1 日
2 件のコメント
Cam Salzberger
2017 年 9 月 3 日
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
2018 年 7 月 31 日
Perfect! That solves my problem too!
Cam Salzberger
2017 年 8 月 31 日
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
カテゴリ
ヘルプ センター および File Exchange で Instrument Control Toolbox についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



