MATLAB Answers

denny
1

Can the matlab app designer support the timer?

Asked by denny
on 31 Aug 2017
Latest activity Commented on by Yao Zhang on 31 Jul 2018
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?

  0 Comments

Sign in to comment.

Products

2 Answers

Answer by 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');
end
methods (Access = private)
% Code that executes after component creation
function startupFcn(app)
app.timer.TimerFcn = @(~, ~) myfunc2(app);
start(app.timer)
end
end
Then the m file.
function myfunc2(app) app.Label.Text=datestr(now);

  2 Comments

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).
Perfect! That solves my problem too!

Sign in to comment.


Answer by 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

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);
start(t)
end
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);
start(t)
end
The following code is the content of the m file.
function myfunc(app)
app.Label.Text=datestr(now);
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
xx=app;
xx.Label.Text=datestr(now);
How can I do it? yinlinfei@163.com

Sign in to comment.