基于Matlab自动化读取处理目录中.txt与.hjson文件并更新结果
Got it, let's build out a full automated workflow for your file processing task—from reading both TXT and HJSON files, running your required calculations, to updating the original TXTs with the new columns. I’ll break this down step by step, building on the TXT loading code you already have.
1. 先准备依赖
First, make sure you have the necessary libraries installed to handle HJSON parsing and tabular data easily:
- Install HJSON parser:
pip install hjson - Install pandas (simplifies TXT table handling):
pip install pandas
2. 加载HJSON文件的工具函数
HJSON is a human-friendly JSON variant—here's a simple function to load and parse any HJSON file into a Python dictionary (assuming your HJSON files hold calculation parameters like time intervals or coordinate system info):
import hjson def load_hjson(file_path): """Load and parse a single HJSON file into a Python dict""" with open(file_path, 'r', encoding='utf-8') as f: return hjson.load(f)
3. 核心计算函数
Next, let's implement the velocity, acceleration, and curvature calculations. I’ll assume your TXT files have columns for timestamp (time), x-coordinate (x), and y-coordinate (y)—adjust the logic if your data structure differs.
计算速度(Velocity)
Velocity is the rate of displacement over time:
import numpy as np def calculate_velocity(x, y, time): """Calculate velocity array from x/y coordinates and timestamps""" dx = np.diff(x) dy = np.diff(y) dt = np.diff(time) velocity = np.sqrt(dx**2 + dy**2) / dt # Pad the first element (we can't calculate velocity for the first data point, use 0 as default) return np.insert(velocity, 0, 0.0)
计算加速度(Acceleration)
Acceleration is the rate of change of velocity:
def calculate_acceleration(velocity, time): """Calculate acceleration array from velocity and timestamps""" dv = np.diff(velocity) dt = np.diff(time) acceleration = dv / dt # Pad the first element with 0 return np.insert(acceleration, 0, 0.0)
计算曲率(Curvature)
Curvature measures how sharply a path turns—we'll use numerical approximation for second derivatives:
def calculate_curvature(x, y): """Calculate curvature array from x/y coordinates""" # First derivatives dx = np.diff(x) dy = np.diff(y) # Second derivatives ddx = np.diff(dx) ddy = np.diff(dy) # Curvature formula: |dx*ddy - dy*ddx| / (dx² + dy²)^(3/2) numerator = np.abs(dx[:-1] * ddy - dy[:-1] * ddx) denominator = (dx[:-1]**2 + dy[:-1]**2)**(1.5) curvature = numerator / denominator # Pad the first two elements with 0 (since we lose two data points from double differentiation) return np.insert(curvature, [0, 0], [0.0, 0.0])
4. 整合完整自动化流程
Now let's tie everything together: we'll traverse all subfolders in Folder 1 for TXT files, load HJSON configs from Folder 2, run calculations, and update the original TXT files with new columns.
import os import pandas as pd def process_all_files(folder1_path, folder2_path): # 1. Load all HJSON configs (merge into a dict keyed by filename) hjson_configs = {} for filename in os.listdir(folder2_path): if filename.endswith('.hjson'): file_path = os.path.join(folder2_path, filename) config_name = filename.split('.')[0] hjson_configs[config_name] = load_hjson(file_path) # 2. Traverse all subfolders in Folder 1 for TXT files for root, _, files in os.walk(folder1_path): for filename in files: if filename.endswith('.txt'): txt_path = os.path.join(root, filename) print(f"Processing {txt_path}...") try: # 3. Load TXT file (adjust sep if your file uses commas/tabs instead of spaces) # If your TXT has no header, use header=None and define columns manually: df.columns = ['time', 'x', 'y', ...] df = pd.read_csv(txt_path, sep='\s+', header=0) # 4. Extract data columns (adjust these to match your TXT's actual column names) time = df['time'].values x = df['x'].values y = df['y'].values # 5. Run calculations and add new columns to the DataFrame df['velocity'] = calculate_velocity(x, y, time) df['acceleration'] = calculate_acceleration(df['velocity'].values, time) df['curvature'] = calculate_curvature(x, y) # 6. Save updated data back to the original TXT # To keep the original file, change the output name to something like filename.replace('.txt', '_processed.txt') df.to_csv(txt_path, sep=' ', index=False, float_format='%.6f') print(f"Successfully updated {txt_path}!") except Exception as e: print(f"Failed to process {txt_path}: {str(e)}") continue # Run the workflow (replace paths with your actual folder locations) if __name__ == "__main__": FOLDER1 = "./folder1" FOLDER2 = "./folder2" process_all_files(FOLDER1, FOLDER2)
5. Quick Adjustments for Your Use Case
- TXT Format Tweaks: If your TXT uses a different separator (commas, tabs) or has no header, modify the
pd.read_csvparameters (e.g.,sep=',',header=None). - HJSON-TXT Matching: If each TXT corresponds to a specific HJSON file (by filename), add logic to map them—e.g.,
matching_hjson = filename.replace('.txt', '.hjson')and load that specific config. - Error Handling: Expand the try-except blocks to catch specific errors (like missing columns, invalid data types) for better debugging.
内容的提问来源于stack exchange,提问作者Alexander Staal

