0

I am having trouble figuring out how to translate the following matlab script to python:

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
%   option # 1 for numeric data only using importdata
    raw = importdata( fullfile(fileDir, fileNames_sorted{f}));
    second_col= [second_col raw(:,2)];  % extract the second column
end
% write all second columns lines into a matrix and store it in excel file 
writematrix(second_col,fullfile(cd,outfile));**

I'm not sure how to translate 'fullfile' and 'natsortfiles' to python functions? This program copies the entire columns from the second row of every excel file in a folder and creates an output file that copies all of the entries into one matrix. Thanks

5
  • 2
    For fullfile you can probably use os.path.join(). natsortfiles could probably be substituted with sorted() or list.sort(). Commented Dec 20, 2021 at 13:09
  • Thanks, I was wondering how I can translate the second_col= [second_col raw(:,2)] and the writematrix(second_col,fullfile(cd,outfile)) to python from matlab? Commented Dec 23, 2021 at 2:05
  • Are you using numpy? Are you familiar with MATLAB but not Python, or with Python but not MATLAB? Commented Jan 5, 2022 at 10:39
  • @nekomatic Yes, Im using numpy, Im familiar with using Matlab but not Python Commented Jan 5, 2022 at 21:55
  • OK so without doing all your work for you :-) I suggest you use np.loadtxt to read each input file, np.hstack to concatenate, and see this answer for saving to .xlsx. For anything else see the numpy docs starting from numpy.org/doc/stable/user/numpy-for-matlab-users.html and if you get stuck, update the question with your Python code and explain what's going wrong. Commented Jan 6, 2022 at 9:36

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.