-1

I was wondering how I can translate the following code into python from matlab script? Especially the content within the for loop and the writematrix last line of code, I have been stuck on. This program codes for copying the second row all columns of every excel file within a folder and creates an output excel file in the same folder which has the second row all columns of every excel file within the folder into a single matrix. Thank you, and a happy new year.

clc
clearvars
fileDir = cd;
outfile = 'OUT.xlsx'; %Output file name
fileNames = dir(fullfile(fileDir,'*.CSV'));
fileNames_sorted = natsortfiles({fileNames.name});
M= length (fileNames_sorted);
second_col= [];

for f = 1:M
    raw = importdata( fullfile(fileDir, fileNames_sorted{f}));
    second_col= [second_col raw(:,2)];  % extract the second column
end
writematrix(second_col,fullfile(cd,outfile))
1

1 Answer 1

0

You have to install packages first

pip install pandas, numpy
from pathlib import Path
import pandas as pd 
import numpy as np

clear 

[del globals()[name] for name in dir() if not name.startswith('_')]

fileDir = Path.cwd()
outfile = Path(fileDir, 'OUT.csv')
fileNames = sorted(fileDir.glob('*.csv'))

second_col= []

for file in fileNames:
    df = pd.read_csv(file)
    second_col.append(df.iloc[:, 0])
    
matrix = np.array(second_col)
np.savetxt(outfile, matrix, delimiter=",")

In case you need to transpose the matrix save like this

np.savetxt(outfile, matrix.T, delimiter=",")

Other ways of saving

Make sure you use tabs in the for loop. Have fun

EDIT

Running dir() within a list comprehension wont work see for explanation dir inside function. Here is an alternative.


from pathlib import Path
import pandas as pd 
import numpy as np

def clearvars():    
    for el in sorted(globals()):
        if '__' not in el:
                print(f'deleted: {el}')
                del el
clearvars() 
clear 

fileDir = Path.cwd()
outfile = Path(fileDir, 'OUT.csv')
fileNames = sorted(fileDir.glob('*.csv'))

second_col= []

for file in fileNames:
    df = pd.read_csv(file)
    second_col.append(df.iloc[:, 0])

matrix = np.array(second_col)
np.savetxt(outfile, matrix, delimiter=",")

You can delete this line print(f'deleted: {el}') if you like. It's just for clarification.

sorted(globals() or dir()contains a dictionary of all variables (namespace) used by python to run that code. Python relevant variables start and end with a '__'. You a basically looping through the names in the namespace and deleting all the names that you created in your script.

Sign up to request clarification or add additional context in comments.

14 Comments

Hi @RSale. I was wondering what the line: [del globals()[name] for name in dir() if not name.startswith('_')] does? Also how can I choose the directory where my program works? thanks
If you are new to python and you are using vscode. Checkout inteactive windows. This will blow your mind ;)
Did the rest of the code work? I don't have Matlab installed right now. So I couldn't varify.
Hi @RSale, Thanks for continuing with your help, unfortunately when I run the code through Spyder it runs the directory through my downloads file outputting an empty excel file. - when I select the directory to be in a specific file directory it returns to the downloads folder unfortunately.
Sorry that was my mistake rglob should be glob``. rglob is the recursive `version. You can also try adding the path as string directly fileDir = "filepath" ``` .
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.