timer オブジェクトを含むス​タンドアロンアプリケ​ーションが期待通りの​動作をしないのはなぜ​ですか?

10 ビュー (過去 30 日間)
MathWorks Support Team
MathWorks Support Team 2023 年 9 月 21 日
回答済み: MathWorks Support Team 2023 年 9 月 21 日
timer オブジェクトを含む .m ファイルを MATLAB Compiler でスタンドアロンアプリケーション化しました。実行させてみると、指定回数の実行前に終了してしまうなど、期待通りの動作となりません。
 
 
testTimer.m:
-------------------------
function testTimer
    i = 1;
    t = timer('TimerFcn', @time_cbf, 'Period', 1, 'ExecutionMode', 'fixedRate', 'TasksToExecute', 10);
    start(t);
    function time_cbf(obj, event)
        t = obj;
        disp(i);
        i = i + 1;
        if i>10
            stop(obj);
        end
    end
end
-------------------------
 
 
MATLAB 環境上での実行例:
-------------------------
>> testTimer
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
-------------------------
 
 
コマンドプロンプトでの実行例(1回のみ実行される):
-------------------------
>testTimer
1
-------------------------
 
 

採用された回答

MathWorks Support Team
MathWorks Support Team 2023 年 9 月 21 日
原因はスタンドアロンアプリケーションがタイマーオブジェクトのstopコマンド実行時にどのようにプログラムを終了させるかによるものです。一般的にはタイマーオブジェクトのコールバック関数はコールバック関数が終了時にスタンドアロンアプリケーションも終了させます。
 
これを回避するためには、メインルーチン内でタイマーオブジェクトのスタート後、MATLABのビルトイン関数であるwaitfor関数を使用し、タイマーオブジェクトが終了するまで待つようにします。
 
修正例:
-------------------------
function testTimer
    i = 1;
    t = timer('TimerFcn', @time_cbf, 'Period', 1, 'ExecutionMode', 'fixedRate', 'TasksToExecute', 10);
    start(t);
    waitfor(t);       %waitforを追加
    function time_cbf(obj, event)
        t = obj;
        disp(i);
        i = i + 1;
        if i>10
            stop(obj);
        end
    end
end
-------------------------
 
 

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeC 共有ライブラリの統合 についてさらに検索

製品


リリース

R2011a

Community Treasure Hunt

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

Start Hunting!