0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

MATLAB Simulinkで取得した波形データをCSVファイルとしてエクスポート

Last updated at Posted at 2025-02-16

はじめに

本記事では、MATLAB Simulinkで取得した波形データをCSVファイルとしてエクスポートし、ダウンロードする方法をまとめます。

Simulinkを使ってシミュレーションを行う際、取得した波形データをPythonやExcelで解析したり、可視化したりしたい場面があります。そこで、本記事ではSimulinkで取得したデータ(timeseries型)をCSVファイルに変換し、エクスポートする方法を解説します。

また、参考にした記事として以下を挙げます。
🔗 MATLAB Central - CSVファイルに出力する方法


使用するコード

以下のコードを使用して、Simulinkのシミュレーション結果をCSVファイルとして保存します。

% Simulinkからの出力 (timeseries 型の場合)
simout = out.simout;

% timeseries データをテーブルに変換
time_data = simout.Time;   % 時間データ
signal_data = simout.Data; % 信号データ

% 時間と信号データを結合してテーブルにする
T = table(time_data, signal_data);

% テーブルを CSV ファイルに保存
writetable(T, 'fileName.csv');

disp('シミュレーション結果が CSV ファイルに保存されました。');

image.png

image.png

image.png

PythonコードでCSVファイルをプロット

from google.colab import files

# アップロードダイアログを表示(CSVファイルを選んでください)
uploaded = files.upload()

import pandas as pd
import matplotlib.pyplot as plt

# アップロードされたファイル名を取得(最初の1個だけ使用)
filename = next(iter(uploaded))

# CSVファイルを読み込み
df = pd.read_csv(filename)

# 必要な列の確認(名前が違う場合は変更してください)
print("Columns in CSV:", df.columns)

# === 追加: 最初のデータと最後のデータを表示 ===
print("\n🔹 最初のデータ(head):")
print(df.head())  # 最初の5行を表示 | Show first 5 rows

print("\n🔹 最後のデータ(tail):")
print(df.tail())  # 最後の5行を表示 | Show last 5 rows

# 時間と信号の列を使ってプロット
time = df["time_data"]
signal = df["signal_data"]

# 波形をプロット
plt.figure(figsize=(10, 4))
plt.plot(time, signal, label="Signal waveform")
plt.xlabel("Time (s)")             # 時間軸(秒)
plt.ylabel("Signal Value")         # 信号の値
plt.title("Waveform over Time")    # グラフのタイトル
plt.grid(True)
plt.legend()
plt.tight_layout()
plt.show()
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?