passing multiple input variables when using pyrunfile
조회 수: 11 (최근 30일)
이전 댓글 표시
I have a python script named "clean_csv.py" that removes selected rows from a CSV and then writes a new CSV:
import pandas as pd
def clean_csv(input_path, output_path, rows_to_remove):
#Load a CSV file, remove specified rows, and save the new CSV
#Parameters:
# input path (str): full path to the input CSV file
# output_path (str): full path to the output CSV file
# rows_to_remove (list of int): Row indices (0-based) to remove
# Load the CSV into a Dataframe
df=pd.read_csv(input_path)
# drop the specified rows
df_cleaned=df.drop(index=rows_to_remove)
# write the cleaned Dataframe to a new CSV
df_cleaned.to_csv(output_path, index=False)
print(f"Cleaned CSV saved to: {output_path}")
I want to call this from MATLAB, while supplying the input_path, output_path and rows_to_remove, e.g.:
input_path = 'D:\datasets\XYZ\XYZ\XYZ.csv';
output_path = 'D:\datasets\ABC\ABC\ABC.csv';
range = [3:5468, 8859:10876];
I am working in the directory where the Py code resides. I am running 2025b with py version 3.11. I have tried some of MATLAB's posted examples and they work.
I have tried a lot of possibilities for calling clean_csv (they have all failed with various or no listed errors), but here is one of the latest attempts:
pyrunfile("clean_csv.py '" + input_path + " " + output_path + "' range")
This one gives no error, but doesn't run
thanks!
댓글 수: 0
답변 (1개)
Taylor
2025년 10월 22일
Have you tried
pyrunfile("clean_csv.py input_path output_path range")
assuming input_path, output_path, and range are defined in the MATLAB workspace?
Also this is pretty straightforward in MATLAB if you don't want to call Python
function clean_csv(input_path, output_path, rows_to_remove)
% Load a CSV file, remove specified rows, and save the new CSV
% Parameters:
% input_path (string): full path to the input CSV file
% output_path (string): full path to the output CSV file
% rows_to_remove (vector): Row indices (1-based) to remove
arguments
input_path (1,1) {mustBeFile}
output_path (1,1) {mustBeFile}
rows_to_remove (1,:) {mustBeNumeric, mustBePositive, mustBeReal, mustBeVector}
end
% Load the CSV into a table
data = readtable(input_path);
% Remove the specified rows
data_cleaned = data;
data_cleaned(rows_to_remove, :) = [];
% Write the cleaned table to a new CSV
writetable(data_cleaned, output_path);
disp("Cleaned CSV saved to: " + output_path);
end
댓글 수: 4
Taylor
2025년 10월 23일
Change your Python code to
import pandas as pd
import sys
import ast
def clean_csv(input_path, output_path, rows_to_remove):
# Your existing function code here
try:
df = pd.read_csv(input_path)
df_cleaned = df.drop(index=rows_to_remove)
df_cleaned.to_csv(output_path, index=False)
print(f"Cleaned CSV saved to: {output_path}")
except Exception as e:
print(f"Error: {e}")
if __name__ == "__main__":
# Check if correct number of arguments provided
if len(sys.argv) != 4:
print("Usage: python clean_csv.py <input_file> <output_file> <rows_to_remove>")
print("Example: python clean_csv.py input.csv output.csv '[0,2,5]'")
sys.exit(1)
input_file = sys.argv[1]
output_file = sys.argv[2]
# Convert string representation of list to actual list
try:
rows_list = ast.literal_eval(sys.argv[3])
except:
print("Error: rows_to_remove must be a valid list format like '[0,1,2]'")
sys.exit(1)
clean_csv(input_file, output_file, rows_list)
and then run this MATLAB code
input_file = "testOriginal.csv";
output_file = "testNew.csv";
rows_to_remove = "[2,3]";
pycode = "clean_csv.py " + input_file + " " + output_file + " " + rows_to_remove;
pyrunfile(pycode)
Also, Stephen is correct, readcell is the correct function to read a text file with hetrogenous data types.
참고 항목
카테고리
Help Center 및 File Exchange에서 Call Python from MATLAB에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!