Can the matlab app designer support the timer?

조회 수: 16 (최근 30일)
denny . 2017년 8월 31일
댓글: Yao Zhang . 2018년 7월 31일
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?

답변 (2개)

denny 2017년 9월 1일
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);
  댓글 수: 2
Yao Zhang
Yao Zhang 2018년 7월 31일
Perfect! That solves my problem too!

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

Cam Salzberger
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);
  댓글 수: 1
denny 2017년 9월 1일
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?

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


Help CenterFile Exchange에서 使用 App 设计工具开发 App에 대해 자세히 알아보기


Community Treasure Hunt

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

Start Hunting!