passing multiple input variables when using pyrunfile

32 views (last 30 days)
Kip
Kip on 22 Oct 2025 at 18:06
Commented: Kip on 23 Oct 2025 at 20:23
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!

Answers (1)

Taylor
Taylor on 22 Oct 2025 at 19:20
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 Comments
Taylor
Taylor on 23 Oct 2025 at 15:26
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.
Kip
Kip on 23 Oct 2025 at 20:23
Well, thank you. This led me further and I now think it's working. Since this is running in a loop and the filenames and rows-to_remove are assigned new in each loop iteration, I found:
the filenames were coming in as chars, so I had to change them to a 1x1 using string(filename)
and I had to make the rows_to_remove a 1x1 also with no whitespace, so I did this:
rows_to_remove = [Startinds Stopinds]; % e.g. Startinds=3:500 and Stopinds = 2500:6000
stringParts = arrayfun(@(x) sprintf('%d,', x), rows_to_remove, 'UniformOutput', false);
resultString = [stringParts{:}]; % concatenate
resultString = resultString(1:end-1); % remove the trailing comma
resultString = strcat('[',resultString,']'); % stick the brackets on the ends
rows_to_remove = string(resultString); % string it
Now it just ran through truncating 963 files in about two minutes :)
Thanks for your help, and Stephen's too.

Sign in to comment.

Products


Release

R2025b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!