現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
Use of timer: what's the code to indicate that its period of time is expired?
2 ビュー (過去 30 日間)
古いコメントを表示
Umberto
2013 年 4 月 3 日
I created a timer with a period of 30 seconds: the program has to execute some tasks if this period is passed. How do i write the code for expressing the fact that the period is passed?
回答 (2 件)
Doug Hull
2013 年 4 月 3 日
編集済み: Doug Hull
2013 年 4 月 3 日
It is not clear what you are asing, but this is a primer on timers.
1 件のコメント
Umberto
2013 年 4 月 4 日
I didnt't find the answer. Here is my code
V = xlsread('data.xls', 1, 'C2:C1002'); %import data from Excel
t = timer('Period', 30, 'TimerFcn', @(~,~)calculate(V, g, n, Vmin, Vmax));
function calculate(V, g, n, Vmin, Vmax)
for i=1:n
if V(i) < Vmin
start(t);
if %30 seconds have passed
V(i) = V(i)+ g;
end
elseif V(i) > Vmax
start(t);
if %30 seconds have passed
V(i) = V(i)- g;
end
elseif (V(i) >= Vmin && V(i) <= Vmax)
stop(t);
end
end
how do I express in code the sentence "30 seconds have passed"?
Sean de Wolski
2013 年 4 月 4 日
One thing you could be do would be to toc a tic that was started earlier. However, the best approach would be to get the InstantPeriod from the timer object
function timerNSeconds
T = timer('Period',10,... %period
'ExecutionMode','fixedRate',... %{singleShot,fixedRate,fixedSpacing,fixedDelay}
'BusyMode','drop',... %{drop, error, queue}
'TasksToExecute',2,...
'StartDelay',0,...
'TimerFcn',@(src,evt)tfcn(src,evt,pi),...
'StartFcn',[],...
'StopFcn',[],...
'ErrorFcn',[]);
start(T);
end
function tfcn(src,evt,piapprox)
pause(10) %comment this line to have it not exceed
pi
if get(src,'InstantPeriod') > 10
disp('Period exceeded 10');
end
end
Uncomment the pause to see it working with not exceeding the 10s.
20 件のコメント
Umberto
2013 年 4 月 5 日
Do I need "src" and "evt" parameters in my code instead of @(~,~) to make it work?
Umberto
2013 年 4 月 5 日
I tried but there's another problem with my code. The error message is "the value assigned to variable 't' might be unused". How can I solve it?
Sean de Wolski
2013 年 4 月 5 日
That's just a code analyzer warning telling you it thinks you're doing something weird. (It tends to be right)
Umberto
2013 年 4 月 8 日
I just can't find out where the problem is, why variable t is unused? I tried
function calculate(t, V, g, n, Vmin, Vmax)
but it doesn't work
Jan
2013 年 4 月 8 日
What does "it doesn't work" exactly mean? We cannot guess these details. We want to help, so please let us and explain the occurring problem.
Sean de Wolski
2013 年 4 月 8 日
What I have above works, so what have you changed to make it not work?
Umberto
2013 年 4 月 8 日
The function "calculate" isn't executed by the program. I know it because i wrote
function calculate(t, V, g, n, Vmin, Vmax)
disp('now executing function');
but i don't see this message in the outputs.
Umberto
2013 年 4 月 9 日
@Sean I changed it because i need to use my algorithm. Now i have this code
function simulation
%%parameters
Vnom = 3850;
s = 0.01;
delay = 30;
g = 50;
n = 1000;
Vmin = Vnom*(1-s);
Vmax = Vnom*(1+s);
V = xlsread('data.xls', 1, 'C2:C1002'); %import data from Excel
t = timer('Period', delay,... %period
'ExecutionMode','fixedRate',... %{singleShot,fixedRate,fixedSpacing,fixedDelay}
'BusyMode','drop',... %{drop, error, queue}
'StartDelay',0,...
'TimerFcn',@(src,evt)calculate(V, g, n, Vmin, Vmax),...
'StartFcn',[],...
'StopFcn',[],...
'ErrorFcn',[]);
end
function calculate(t, src, V, g, n, Vmin, Vmax)
disp('now executing function');
for i=1:n
if V(i) < Vmin
start(t);
if get(src,'InstantPeriod') > delay
V(i) = V(i)+ g;
end
elseif V(i) > Vmax
start(t);
if get(src,'InstantPeriod') > delay
V(i) = V(i)- g;
end
elseif (V(i) >= Vmin && V(i) <= Vmax)
stop(t);
end
end
xlswrite('data.xls', V, 1, 'E2:E1002');
end
Also you put start(t) before the timer function: i need to start the timer only if V(i) < Vmin or if V(i) > Vmax
Sean de Wolski
2013 年 4 月 9 日
The problem looks like the calling syntax for setting up the timerfcn is different than the calling syntax for calculate().
Sean de Wolski
2013 年 4 月 10 日
Use the same calling syntax...
calculate(t, src, V, g, n, Vmin, Vmax)
v.
calculate(V, g, n, Vmin, Vmax),...
Umberto
2013 年 4 月 11 日
I used the same syntax calculate (V, g, n, Vmin, Vmax) but I still have the same problem.
Jan
2013 年 4 月 11 日
@Umberto: Please do not let us guess, what your problem is. Formerly you told us, that there is the message, that "t" is unused. Now you have removed the "t" and get the same message? This is impossible. So please do not assume, that we use crystal balls, but explain the problem exactly and post the relevant part of the code.
Umberto
2013 年 4 月 11 日
function simulation
V = xlsread('data.xls', 1, 'C2:C1002'); %import data from Excel
t = timer('Period', delay,... %period
'ExecutionMode','fixedRate',... %{singleShot,fixedRate,fixedSpacing,fixedDelay}
'BusyMode','drop',... %{drop, error, queue}
'StartDelay',0,...
'TimerFcn',@(src,evt)calculate(V, g, n, Vmin, Vmax),...
'StartFcn',[],...
'StopFcn',[],...
'ErrorFcn',[]);
end
function calculate(V, g, n, Vmin, Vmax)
disp('now executing function');
for i=1:n
if V(i) < Vmin
start(t);
if get(src,'InstantPeriod') > delay
V(i) = V(i)+ g;
end
elseif V(i) > Vmax
start(t);
if get(src,'InstantPeriod') > delay
V(i) = V(i)- g;
end
elseif (V(i) >= Vmin && V(i) <= Vmax)
stop(t);
end
end
I have 2 problems:
1)error message "variable t might be unused"
2)the function calculate isn't executed by the program because I don't see the message 'now executing function' in the outputs.
Sean de Wolski
2013 年 4 月 11 日
So what you're trying to do here isn;t working for a few reasons.
First, t is scoped to the parent function simulation so that is why it does not exist in the subfunction calculate.
Second, I don't understand why you want to start the timer inside of its timerfcn. That doesn't make sense to me.
Third, you are not passing src into calculate(). Thus it will error, you need to pass in src like I did in my original.
Umberto
2013 年 4 月 11 日
Ok I understood what you mean and I made some changes: I passed src into calculate() and started the timer before calculate(). However i need to use t into function calculate and I didn't manage to do it, i get "Undefined function or variable 't'" error message. Is there a way solve this problem?
Umberto
2013 年 4 月 12 日
But if I pass src to calculate() then why I get the "Undefined function or variable 't'" message?
Sean de Wolski
2013 年 4 月 12 日
Because you don't pass t!!!! You've renamed it to src even though it is the same thing. You call it t instead if you wish; it's just a good programming practice to call the handle of the source object of a callback src or similar.
参考
カテゴリ
Help Center および File Exchange で Environment and Settings についてさらに検索
タグ
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
アジア太平洋地域
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)