Created
November 14, 2024 05:57
-
-
Save hctsai1006/0f8446d911eb4d2a88eafc9bd8715b4d to your computer and use it in GitHub Desktop.
mat_to_csv_converter.ipynb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "nbformat": 4, | |
| "nbformat_minor": 0, | |
| "metadata": { | |
| "colab": { | |
| "provenance": [], | |
| "authorship_tag": "ABX9TyOTetuPvLuqfGg8LxxwwXRg", | |
| "include_colab_link": true | |
| }, | |
| "kernelspec": { | |
| "name": "python3", | |
| "display_name": "Python 3" | |
| }, | |
| "language_info": { | |
| "name": "python" | |
| } | |
| }, | |
| "cells": [ | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "id": "view-in-github", | |
| "colab_type": "text" | |
| }, | |
| "source": [ | |
| "<a href=\"https://colab.research.google.com/gist/hctsai1006/0f8446d911eb4d2a88eafc9bd8715b4d/mat_to_csv_converter.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": { | |
| "id": "c9u_MU95ITZo" | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "# 匯入 lib、package 之類的\n", | |
| "import os\n", | |
| "import glob\n", | |
| "import re # 匯入正則表示式模組\n", | |
| "from scipy.io import loadmat\n", | |
| "import pandas as pd\n", | |
| "from google.colab import drive\n", | |
| "import numpy as np\n", | |
| "\n", | |
| "# 掛接 Google Drive(colab 右方導航欄也可以手動掛接)\n", | |
| "# drive.mount('/content/drive')\n", | |
| "\n", | |
| "# 設定(.mat)檔的輸入和輸出 csv 資料夾的路徑\n", | |
| "# 輸入資料夾路徑(在 Google Drive 中,放 .mat 檔的資料夾)\n", | |
| "input_folder = '/content/drive/MyDrive/XXXXX/' # 請依你的實際情況調整\n", | |
| "\n", | |
| "# 輸出資料夾路徑(在 Google Drive 中,用來儲存轉換後的 CSV 檔的資料夾(注意:這行指令會新增一個名叫 \"converted_csv\" 資料夾喔!))\n", | |
| "output_folder = '/content/drive/MyDrive/XXXXX/converted_csv' # 請依你的實際情況調整\n", | |
| "\n", | |
| "# 如果輸出資料夾不存在,就創建它\n", | |
| "os.makedirs(output_folder, exist_ok=True)\n", | |
| "\n", | |
| "# 獲取所有 .mat 檔的路徑\n", | |
| "mat_files = glob.glob(os.path.join(input_folder, '*.mat'))\n", | |
| "\n", | |
| "# 遍歷所有 .mat 檔\n", | |
| "for mat_file in mat_files:\n", | |
| " # 載入 .mat 檔\n", | |
| " mat_data = loadmat(mat_file, struct_as_record=False, squeeze_me=True)\n", | |
| "\n", | |
| " # 獲取檔名(不包含副檔名),並直接使用檔名\n", | |
| " base_name = os.path.splitext(os.path.basename(mat_file))[0]\n", | |
| "\n", | |
| " # 提取資料變數(排除特殊鍵)\n", | |
| " variable_names = [key for key in mat_data.keys() if not key.startswith('__')]\n", | |
| "\n", | |
| " # 如果沒有找到變數,跳過該檔\n", | |
| " if not variable_names:\n", | |
| " print(f\"檔案 {mat_file} 中沒有找到資料變數,已跳過。\")\n", | |
| " continue\n", | |
| "\n", | |
| " # 遍歷所有變數\n", | |
| " for var_name in variable_names:\n", | |
| " data = mat_data[var_name]\n", | |
| "\n", | |
| " # 檢查資料型態並處理\n", | |
| " if isinstance(data, (np.ndarray, list)):\n", | |
| " # 如果資料是多維陣列,可能需要展平或重新塑形\n", | |
| " if isinstance(data, np.ndarray) and data.ndim > 2:\n", | |
| " data = data.reshape(data.shape[0], -1)\n", | |
| "\n", | |
| " # 轉換為 DataFrame\n", | |
| " df = pd.DataFrame(data)\n", | |
| " elif isinstance(data, (int, float, str)):\n", | |
| " # 如果資料是單個值,轉換為 DataFrame\n", | |
| " df = pd.DataFrame([data])\n", | |
| " else:\n", | |
| " # 如果資料是 structure( MATLAB資料容器),可能需要特殊處理\n", | |
| " print(f\"變數 {var_name} 是一個複雜的資料型態,可能需要手動處理。\")\n", | |
| " continue\n", | |
| "\n", | |
| " # 使用原始檔名,改副檔名為 .csv\n", | |
| " output_file = f\"{base_name}.csv\"\n", | |
| " output_path = os.path.join(output_folder, output_file)\n", | |
| "\n", | |
| " # 儲存為 CSV 檔\n", | |
| " df.to_csv(output_path, index=False)\n", | |
| "\n", | |
| " print(f\"已將變數 {var_name} 轉換為 {output_file}\")\n", | |
| "\n", | |
| "print(\"所有檔案已處理完成,轉換後的 CSV 檔儲存在以下路徑:\")\n", | |
| "print(output_folder)\n" | |
| ] | |
| } | |
| ] | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
從 Google Drive 讀取 .mat 檔案(一種 MATLAB 的資料檔案格式),並將 .mat files 轉換為 CSV 格式並儲回 Google Drive 中(會建立一個名為 "converted_csv" 的資料夾)