Extracting one column from multiple csv files into one with a mix of numbers and variables

7 views (last 30 days)
Hi everyone, I am new to matlab, and would really appreciate some help. I have roughly 100 csv files with a mix of numbers and variables. All the files have the same format in term of number of columns but with variable number of rows, and I need to extract the numerical data from a specific column (E-'PathLength') from each, without the header itself, and copy the data to a new file which will contain the filename as the header and the data underneath it. I am attaching a sample file. Thank you so much!
  1 Comment
oded mayseless
oded mayseless on 16 Apr 2018
here is the code I have so far...
myDir = uigetdir; %gets directory
ds = tabularTextDatastore(myDir,'FileExtensions','.csv');%get a list of all the csv files in the folder
fileNames = (ds.Files); %generate a variable with the actual file names
for k = 1:numel(fileNames)
fid = readtable(fileNames(k)); % read the file in position 'k' in the fileNames array
PathLength = fid(:,5); % read the 5th column from the csv file
write = {fileNames(k), PathLength}; % make an array of what I want to write to an excell file
xlswrite('data',write,(A,k)); %write it in an excell file in column a row(
end

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!