メインコンテンツ

このページは機械翻訳を使用して翻訳されました。最新版の英語を参照するには、ここをクリックします。

MDF データストアと tall 配列を使用してデータを解析する

この例では、tall 配列と MDF データストア機能を使用して大きなデータ セットを操作する方法を示します。tall 配列は、メモリに収まらないさまざまな種類のデータに対して計算を実行するためによく使用されます。

この例では、最初にデータの小さなサブセットを操作し、その後スケールアップしてデータセット全体を解析します。ここで使用したデータ セットは実際のアプリケーションにおける実際のサイズを反映していない可能性がありますが、同じ解析手法をさらに拡張して、メモリに読み込むことができないほど大きなデータ セットを処理することもできます。

tall 配列の詳細については、例 tall 配列を使用した MATLAB でのビッグ データの解析 を参照してください。

tall 配列の紹介

tall 配列と tall table は、任意の行数をもつメモリに収まらないデータを処理するために使用されます。tall 配列と table を使用すると、メモリ内の MATLAB 配列と同様の方法で大規模なデータ セットを操作できます。

違いは、計算の実行が要求されるまで、通常、tall 配列は評価されないままであることです。この遅延評価により、MATLAB では可能な限りキューに登録された計算を組み合わせ、データを通す回数を最小に抑えることができます。

MDFデータストアを作成する

MDF データストアを使用すると、複数の MDF ファイルに保存されている同種のデータを単一のエンティティとして読み取って処理できます。データ セットが大きすぎてメモリに収まらない場合は、データストアを使用すると、メモリに個別に収まる小さなブロックでデータ セットを操作することもできます。この関数は、共通の関数を使用してデータストアによってバックアップされたメモリ外のデータを操作できるようにする、tall 配列によってさらに拡張できます。

現在のワークフロー ディレクトリで MDF ファイル EngineData_MDF_TallArray.mf4 を選択し、mdfDatastore 関数を使用して MDF データストアを作成します。このファイルには、ダイナモメーターに接続されたエンジン プラントとコントローラーを表す Simulink モデルから記録されたタイムスタンプ付きのデータが含まれています。

mds = mdfDatastore("EngineData_MDF_TallArray.mf4")
mds = 
  MDFDatastore with properties:

  Datastore Details
                         Files: {
                                ' ...\michellw.Bdoc24a_MDF\vnt-ex08773747\EngineData_MDF_TallArray.mf4'
                                }
                 ChannelGroups: 
                                  GroupNumber    AcquisitionName     Comment          ... and 10 more columns    
                                  ___________    _______________    __________                

                                       1         {[<undefined>]}    {[Python]}                


                      Channels: 
                                       Name          GroupNumber    DisplayName        ... and 17 more columns    
                                  _______________    ___________    ___________                

                                  "EngineSpeed"           1             ""                     
                                  "EngineTorque"          1             ""                     
                                  "TorqueCommand"         1             ""                     

                                ... and 1 more rows

  Options
          SelectedChannelNames: {
                                'EngineSpeed';
                                'EngineTorque';
                                'TorqueCommand'
                                 ... and 1 more
                                }
    SelectedChannelGroupNumber: 1
                      ReadSize: "file"
                       ReadRaw: 0

MDF データストアをさらに構成して、MDF ファイルから読み取るデータの内容と方法を制御することもできます。デフォルトでは、最初のチャネル グループが選択され、そのグループのすべてのチャネルが読み取られます。

mds.SelectedChannelGroupNumber
ans = 1
mds.SelectedChannelNames
ans = 4×1 string
    "EngineSpeed"
    "EngineTorque"
    "TorqueCommand"
    "t"

関心のある 3 つの変数 (EngineSpeedTorqueCommandEngineTorque) のみを選択するように MDF データストアを構成します。

mds.SelectedChannelNames = ["EngineSpeed", "TorqueCommand", "EngineTorque"]
mds = 
  MDFDatastore with properties:

  Datastore Details
                         Files: {
                                ' ...\michellw.Bdoc24a_MDF\vnt-ex08773747\EngineData_MDF_TallArray.mf4'
                                }
                 ChannelGroups: 
                                  GroupNumber    AcquisitionName     Comment          ... and 10 more columns    
                                  ___________    _______________    __________                

                                       1         {[<undefined>]}    {[Python]}                


                      Channels: 
                                       Name          GroupNumber    DisplayName        ... and 17 more columns    
                                  _______________    ___________    ___________                

                                  "EngineSpeed"           1             ""                     
                                  "EngineTorque"          1             ""                     
                                  "TorqueCommand"         1             ""                     

                                ... and 1 more rows

  Options
          SelectedChannelNames: {
                                'EngineSpeed';
                                'TorqueCommand';
                                'EngineTorque'
                                }
    SelectedChannelGroupNumber: 1
                      ReadSize: "file"
                       ReadRaw: 0

preview 関数を使用して選択したデータをプレビューします。

preview(mds)
ans=8×3 timetable
          t           EngineSpeed    TorqueCommand    EngineTorque
    ______________    ___________    _____________    ____________

    0 sec                     0              0           47.153   
    0 sec              2.37e-26              0           47.153   
    1.47e-05 sec        0.11056         47.158           47.158   
    8.85e-05 sec        0.66312         48.708           48.708   
    0.00010107 sec      0.75762          49.77            49.77   
    0.00010107 sec      0.75762          49.77            49.77   
    0.0001405 sec         1.053         39.967           39.967   
    0.00017993 sec       1.3482         23.143           23.143   

tall 配列の作成

tall 配列は、任意の行数をもつことができる点を除き、インメモリ MATLAB 配列と似ています。MDF データストア mds にはタイムスタンプ付きの表形式のデータが含まれているため、tall 関数はデータストアのデータを含む tall timetable を返します。

tt = tall(mds)
tt =

  M×3 tall timetable

          t           EngineSpeed    TorqueCommand    EngineTorque
    ______________    ___________    _____________    ____________

    0 sec                     0              0           47.153   
    0 sec              2.37e-26              0           47.153   
    1.47e-05 sec        0.11056         47.158           47.158   
    8.85e-05 sec        0.66312         48.708           48.708   
    0.00010107 sec      0.75762          49.77            49.77   
    0.00010107 sec      0.75762          49.77            49.77   
    0.0001405 sec         1.053         39.967           39.967   
    0.00017993 sec       1.3482         23.143           23.143   
          :                :               :               :
          :                :               :               :

表示には最初の数行のデータが含まれます。timetable のサイズは、行数がまだ MATLAB に認識されていないことを示すために M×3 と表示される場合があります。

tall 配列で計算を実行する

メモリ内の MATLAB 配列やテーブルと同様に、tall 配列や tall table を操作できます。ただし、MATLAB は、tall 配列に対してほとんどの操作を実行せず、出力が要求されるまで実際の計算を延期します。

評価されていない tall 配列を操作し、必要な場合にのみ出力を要求するのが一般的です。MATLAB は、評価されて表示されるように要求されるまで、評価されていない tall 配列の内容やサイズを認識しません。

TorqueCommand 変数の中央値、最小値、最大値を計算します。結果はすぐには評価されないことに注意してください。

medianTorqueCommand = median(tt.TorqueCommand)
medianTorqueCommand =

  tall double

    ?

Preview deferred. Learn more.
minTorqueCommand = min(tt.TorqueCommand)
minTorqueCommand =

  tall double

    ?

Preview deferred. Learn more.
maxTorqueCommand = max(tt.TorqueCommand)
maxTorqueCommand =

  tall double

    ?

Preview deferred. Learn more.

結果をワークスペースに収集

関数 gather によって、キューに登録されたすべての演算が強制的に評価されて、結果の出力はメモリに戻ります。

キューに入れられた操作 medianminmax を実行し、回答を評価します。計算にデータの複数回のパスが必要な場合、MATLAB は実行時間を節約するためにパスの最小数を決定し、この情報をコマンド ラインに表示します。

[medianTorqueCommand, minTorqueCommand, maxTorqueCommand] = gather(medianTorqueCommand, minTorqueCommand, maxTorqueCommand)
Evaluating tall expression using the Parallel Pool 'Processes':
- Pass 1 of 4: Completed in 0.74 sec
- Pass 2 of 4: Completed in 0.37 sec
- Pass 3 of 4: Completed in 0.61 sec
- Pass 4 of 4: Completed in 0.47 sec
Evaluation completed in 3.2 sec
medianTorqueCommand = 116.2799
minTorqueCommand = 0
maxTorqueCommand = 232.9807

tall 配列のサブセットを選択

head を使用して、完全なデータ セットにスケーリングする前に、コードのプロトタイピング用にデータから 10,000 行のサブセットを選択します。

ttSubset = head(tt, 10000)
ttSubset =

  10,000×3 tall timetable

          t           EngineSpeed    TorqueCommand    EngineTorque
    ______________    ___________    _____________    ____________

    0 sec                     0              0           47.153   
    0 sec              2.37e-26              0           47.153   
    1.47e-05 sec        0.11056         47.158           47.158   
    8.85e-05 sec        0.66312         48.708           48.708   
    0.00010107 sec      0.75762          49.77            49.77   
    0.00010107 sec      0.75762          49.77            49.77   
    0.0001405 sec         1.053         39.967           39.967   
    0.00017993 sec       1.3482         23.143           23.143   
          :                :               :               :
          :                :               :               :

tall 配列内の重複行を削除する

timetable の行は、行時間とデータ値が同じ場合に重複となります。unique 関数を使用して、サブセット tall timetable から重複する行を削除します。

ttSubset = unique(ttSubset)
ttSubset =

  9,968×3 tall timetable

          t           EngineSpeed    TorqueCommand    EngineTorque
    ______________    ___________    _____________    ____________

    0 sec                     0              0            47.153  
    0 sec              2.37e-26              0            47.153  
    1.47e-05 sec        0.11056         47.158            47.158  
    8.85e-05 sec        0.66312         48.708            48.708  
    0.00010107 sec      0.75762          49.77             49.77  
    0.0001405 sec         1.053         39.967            39.967  
    0.00017993 sec       1.3482         23.143            23.143  
    0.00037708 sec       2.8228         23.143         -0.021071  
          :                :               :               :
          :                :               :               :

エンジン出力を計算する

P [kW]=πN [rpm]T [Nm]301000 を使用して、EngineSpeedEngineTorque でエンジン出力をキロワット (kW) 単位で計算します。結果を、tall timetable 内の EnginePower という新しい変数に保存します。

ttSubset.EnginePower = (pi * ttSubset.EngineSpeed .* ttSubset.EngineTorque) / (30 * 1000)
ttSubset =

  9,968×4 tall timetable

          t           EngineSpeed    TorqueCommand    EngineTorque    EnginePower
    ______________    ___________    _____________    ____________    ___________

    0 sec                     0              0            47.153                0
    0 sec              2.37e-26              0            47.153       1.1703e-28
    1.47e-05 sec        0.11056         47.158            47.158       0.00054599
    8.85e-05 sec        0.66312         48.708            48.708        0.0033824
    0.00010107 sec      0.75762          49.77             49.77        0.0039487
    0.0001405 sec         1.053         39.967            39.967        0.0044072
    0.00017993 sec       1.3482         23.143            23.143        0.0032675
    0.00037708 sec       2.8228         23.143         -0.021071      -6.2287e-06
          :                :               :               :               :
          :                :               :               :               :

tall 配列の topkrows 関数は、ソートされた順序で上位の k 行を返します。最大の EnginePower 値を持つ上位 20 行を取得します。

maxEnginePower = topkrows(ttSubset, 20, "EnginePower")
maxEnginePower =

  20×4 tall timetable

        t        EngineSpeed    TorqueCommand    EngineTorque    EnginePower
    _________    ___________    _____________    ____________    ___________

    15.17 sec        750           78.052           78.052         6.1302   
    15.16 sec        750           77.841           77.841         6.1136   
    15.15 sec        750           77.556           77.556         6.0912   
    15.14 sec        750           77.326           77.326         6.0732   
    15.18 sec        750           77.277           77.277         6.0693   
    15.13 sec        750           77.157           77.157         6.0599   
    15.12 sec        750           77.082           77.082          6.054   
    15.11 sec        750           77.067           77.075         6.0534   
        :             :               :               :               :
        :             :               :               :               :

gather 関数を呼び出して、キューに入れられたすべての操作を実行し、結果をメモリに収集します。

[ttSubset, maxEnginePower] = gather(ttSubset, maxEnginePower)
ttSubset=9968×4 timetable
          t           EngineSpeed    TorqueCommand    EngineTorque    EnginePower
    ______________    ___________    _____________    ____________    ___________

    0 sec                     0              0            47.153                0
    0 sec              2.37e-26              0            47.153       1.1703e-28
    1.47e-05 sec        0.11056         47.158            47.158       0.00054599
    8.85e-05 sec        0.66312         48.708            48.708        0.0033824
    0.00010107 sec      0.75762          49.77             49.77        0.0039487
    0.0001405 sec         1.053         39.967            39.967        0.0044072
    0.00017993 sec       1.3482         23.143            23.143        0.0032675
    0.00037708 sec       2.8228         23.143         -0.021071      -6.2287e-06
    0.00076951 sec       5.7492             15         -0.042938      -2.5851e-05
    0.0014014 sec        10.437             15         -0.078013      -8.5265e-05
    0.0023449 sec        17.382             15          -0.13009      -0.00023679
    0.0036773 sec        27.079             15          -0.20304      -0.00057575
    0.0054808 sec            40             15          -0.30067       -0.0012595
    0.0072843 sec        52.691             15          -0.39703       -0.0021907
    0.01 sec             71.373             15          -0.53973       -0.0040341
    0.013562 sec         95.119             15            51.176          0.50976
      ⋮

maxEnginePower=20×4 timetable
        t        EngineSpeed    TorqueCommand    EngineTorque    EnginePower
    _________    ___________    _____________    ____________    ___________

    15.17 sec        750           78.052           78.052         6.1302   
    15.16 sec        750           77.841           77.841         6.1136   
    15.15 sec        750           77.556           77.556         6.0912   
    15.14 sec        750           77.326           77.326         6.0732   
    15.18 sec        750           77.277           77.277         6.0693   
    15.13 sec        750           77.157           77.157         6.0599   
    15.12 sec        750           77.082           77.082          6.054   
    15.11 sec        750           77.067           77.075         6.0534   
    15.1 sec         750           77.067           77.067         6.0528   
    15.09 sec        750           77.059           77.059         6.0522   
    15.08 sec        750           77.051           77.051         6.0516   
    15.07 sec        750           77.042           77.042         6.0509   
    15.06 sec        750           77.034           77.034         6.0502   
    15.05 sec        750           77.025           77.025         6.0495   
    15.04 sec        750           77.016           77.016         6.0488   
    15.03 sec        750           77.006           77.006         6.0481   
      ⋮

tall 配列でデータを可視化する

2 つの Y 軸を持つプロットで、時間の経過に伴う EngineTorque 信号と EnginePower 信号を可視化します。

figure
yyaxis left
plot(ttSubset.t, ttSubset.EngineTorque)
title("Engine Torque and Engine Power Over Time")
xlabel("Time")
ylabel("Engine Torque [Nm]")

yyaxis right
plot(ttSubset.t, ttSubset.EnginePower)
ylabel("Engine Power [kW]")

Figure contains an axes object. The axes object with title Engine Torque and Engine Power Over Time, xlabel Time, ylabel Engine Power [kW] contains 2 objects of type line.

データ セット全体へのスケーリング

head から返された小さなデータを使用する代わりに、完全な tall timetable を使用して、データ セット全体に同じ手順を適用するようにスケールアップします。

tt = tall(mds)
tt =

  M×3 tall timetable

          t           EngineSpeed    TorqueCommand    EngineTorque
    ______________    ___________    _____________    ____________

    0 sec                     0              0           47.153   
    0 sec              2.37e-26              0           47.153   
    1.47e-05 sec        0.11056         47.158           47.158   
    8.85e-05 sec        0.66312         48.708           48.708   
    0.00010107 sec      0.75762          49.77            49.77   
    0.00010107 sec      0.75762          49.77            49.77   
    0.0001405 sec         1.053         39.967           39.967   
    0.00017993 sec       1.3482         23.143           23.143   
          :                :               :               :
          :                :               :               :

まず、tall timetable から重複した行を削除します。

tt = unique(tt)
tt =

  M×3 tall timetable

    t    EngineSpeed    TorqueCommand    EngineTorque
    _    ___________    _____________    ____________

    ?         ?               ?               ?      
    ?         ?               ?               ?      
    ?         ?               ?               ?      
    :         :               :               :
    :         :               :               :

Preview deferred. Learn more.

次に、エンジン出力を計算し、最大の EnginePower 値を持つ上位 20 行を取得します。

tt.EnginePower = (pi * tt.EngineSpeed .* tt.EngineTorque) / (30 * 1000)
tt =

  M×4 tall timetable

    t    EngineSpeed    TorqueCommand    EngineTorque    EnginePower
    _    ___________    _____________    ____________    ___________

    ?         ?               ?               ?               ?     
    ?         ?               ?               ?               ?     
    ?         ?               ?               ?               ?     
    :         :               :               :               :
    :         :               :               :               :

Preview deferred. Learn more.
maxEnginePower = topkrows(tt, 20, "EnginePower")
maxEnginePower =

  M×4 tall timetable

    t    EngineSpeed    TorqueCommand    EngineTorque    EnginePower
    _    ___________    _____________    ____________    ___________

    ?         ?               ?               ?               ?     
    ?         ?               ?               ?               ?     
    ?         ?               ?               ?               ?     
    :         :               :               :               :
    :         :               :               :               :

Preview deferred. Learn more.
[tt, maxEnginePower] = gather(tt, maxEnginePower)
Evaluating tall expression using the Parallel Pool 'Processes':
- Pass 1 of 1: Completed in 0.95 sec
Evaluation completed in 1.4 sec
tt=359326×4 timetable
          t           EngineSpeed    TorqueCommand    EngineTorque    EnginePower
    ______________    ___________    _____________    ____________    ___________

    0 sec                     0              0            47.153                0
    0 sec              2.37e-26              0            47.153       1.1703e-28
    1.47e-05 sec        0.11056         47.158            47.158       0.00054599
    8.85e-05 sec        0.66312         48.708            48.708        0.0033824
    0.00010107 sec      0.75762          49.77             49.77        0.0039487
    0.0001405 sec         1.053         39.967            39.967        0.0044072
    0.00017993 sec       1.3482         23.143            23.143        0.0032675
    0.00037708 sec       2.8228         23.143         -0.021071      -6.2287e-06
    0.00076951 sec       5.7492             15         -0.042938      -2.5851e-05
    0.0014014 sec        10.437             15         -0.078013      -8.5265e-05
    0.0023449 sec        17.382             15          -0.13009      -0.00023679
    0.0036773 sec        27.079             15          -0.20304      -0.00057575
    0.0054808 sec            40             15          -0.30067       -0.0012595
    0.0072843 sec        52.691             15          -0.39703       -0.0021907
    0.01 sec             71.373             15          -0.53973       -0.0040341
    0.013562 sec         95.119             15            51.176          0.50976
      ⋮

maxEnginePower=20×4 timetable
        t         EngineSpeed    TorqueCommand    EngineTorque    EnginePower
    __________    ___________    _____________    ____________    ___________

    3819.8 sec       5000           217.53           217.53          113.9   
    3819.8 sec       5000           217.53           217.53          113.9   
    3819.8 sec       5000           217.53           217.53          113.9   
    3819.8 sec       5000           217.53           217.53          113.9   
    3819.8 sec       5000           217.53           217.53          113.9   
    3819.9 sec       5000           217.53           217.53          113.9   
    3819.9 sec       5000           217.53           217.53          113.9   
    3819.9 sec       5000           217.53           217.53          113.9   
    3819.9 sec       5000           217.52           217.52         113.89   
    3819.9 sec       5000           217.52           217.52         113.89   
    3820 sec         5000           217.52           217.52         113.89   
    3820.1 sec       5000           217.52           217.52         113.89   
    3820.2 sec       5000           217.52           217.52         113.89   
    3820.3 sec       5000           217.52           217.52         113.89   
    3820.4 sec       5000           217.52           217.52         113.89   
    3820.5 sec       5000           217.52           217.52         113.89   
      ⋮

最後に、2 つの Y 軸を持つプロットで、EngineTorque 信号と EnginePower 信号を時間の経過に沿って可視化します。

figure
yyaxis left
plot(tt.t, tt.EngineTorque)
title("Engine Torque and Engine Power Over Time")
xlabel("Time")
ylabel("Engine Torque [Nm]")

yyaxis right
plot(tt.t, tt.EnginePower)
ylabel("Engine Power [kW]")

Figure contains an axes object. The axes object with title Engine Torque and Engine Power Over Time, xlabel Time, ylabel Engine Power [kW] contains 2 objects of type line.

MDFファイルを閉じる

ワークスペースから MDF データストア変数をクリアして、MDF ファイルへのアクセスを閉じます。

clear mds