App Designer にて、Timer オブジェクトのコール​バックを使用するには​どうすればよいですか​?

7 ビュー (過去 30 日間)
MathWorks Support Team
MathWorks Support Team 2017 年 6 月 29 日
編集済み: MathWorks Support Team 2020 年 7 月 7 日
App Designer で GUI を構築しています。その中で、Timer オブジェクトを使って、コールバックを設定したいのですが、エラーが発生して実行できません。
Timer オブジェクトは、コールバック間で共有できるように、プライベートプロパティとして設定し、UIFigure の StartFcn で Timer オブジェクトを定義しています。
Timer オブジェクトの TimerFcn コールバックは、App Designer のプライベート関数として記述していますが、Timer をスタートさせると、TimerFcn コールバックとして定義した関数が未定義というエラーが発生します。
(コード抜粋)
methods (Access = private)
function mytimer_fun(app)
% Timer で指定するコールバック関数
disp('Timer!!!!')
end
end
properties (Access = private)
% Timer object 用
update_timer
end
methods (Access = private)
% Code that executes after component creation
function startupFcn(app)
app.update_timer = timer('Period', 1,...
'ExecutionMode', 'fixedSpacing', ...
'TasksToExecute', Inf, ...
'TimerFcn', @mytimer_fun); %コールバック
end
% Button pushed function: STARTButton
function STARTButtonPushed(app, event)
start(app.update_timer)
end
% Button pushed function: STOPButton
function STOPButtonPushed(app, event)
stop(app.update_timer)
delete(app.update_timer)
end
end
(エラーの例) ERROR: TimerFcn (タイマー 'timer-12') を実行中にエラーが発生 関数 'mytimer_fun' (タイプ'timer' の入力引数) が未定義です。

採用された回答

MathWorks Support Team
MathWorks Support Team 2020 年 7 月 9 日
編集済み: MathWorks Support Team 2020 年 7 月 7 日
AppDesigner で timer オブジェクト・コールバックを実装するポイントとして、以下の 2 点があります。
1. Timer オブジェクトのコールバック関数の入力引数には、次の3 つ(app, obj, event) が必要です。
function mytimer_fun(app, obj, event)
このとき、'app' は、app オブジェクトのハンドル、'obj' は、timer オブジェクトのハンドル、’event’ はtimer イベントのためのイベントデータです。
さらにTimer のコールバック関数に入力を与えたい場合には、第4入力引数として与えます。
function mytimer_fun(app, obj, event, string_arg)
2. App 内のプライベート関数を Timer オブジェクトのコールバックに指定したい場合には、"@app.mytimer_fun" のように "app" を付けて指定します。
例:
function startupFcn(app)
app.update_timer = timer('Period', 1,...
'ExecutionMode', 'fixedSpacing', ...
'TasksToExecute', Inf, ...
'TimerFcn', @app.mytimer_fun); %コールバック
end
質問文にあるコマンドを修正すると次のとおりです。
methods (Access = private)
function mytimer_fun(*app, obj, event*)
% Timer で指定するコールバック関数
disp('Timer!!!!')
end
end
properties (Access = private)
% Timer object 用
update_timer
end
methods (Access = private)
% Code that executes after component creation
function startupFcn(app)
app.update_timer = timer('Period', 1,...
'ExecutionMode', 'fixedSpacing', ...
'TasksToExecute', Inf, ...
'TimerFcn', *@app.mytimer_fun*); %コールバック
end
% Button pushed function: STARTButton
function STARTButtonPushed(app, event)
start(app.update_timer)
end
% Button pushed function: STOPButton
function STOPButtonPushed(app, event)
stop(app.update_timer)
delete(app.update_timer)
end
end
関連する内容が、下記 URL からご覧いただけます。
・プログラムによる App Designer への UI コンポーネントの追加

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeApp Designer を使用したアプリ開発 についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!