Extracting specific data from multiple excel files to create a single excel file on MAC
Show older comments
Hi all I have a folder on my computer with 300 excel files and I don't want to manually extract the middle row from every excel file manually and combine into a single excel file. The rows differ in each file ranging from 4-6, if the row amount is 4 I would like to get the second row.
I was wondering how I can do this on Matlab through reading every Excel file in that specific file and taking out all of the columns from the middle row and then combining all that into a excel file?
I feel like some of the fucntions on a mac are limited compared to a PC.
Thanks in advance for the help!
Answers (1)
Image Analyst
on 19 Jul 2022
1 vote
Try the FAQ:
In the middle of the loop, use readmatrix to read in the file and then extract the row(s) you want into a new array. Then after the loop, write out the new array with writematrix
Write back if you can't figure it out and attach a couple of your workbooks with the paperclip icon after reading this:
13 Comments
Walter Roberson
on 19 Jul 2022
or readtable() if you have mixed data types
Walter Roberson
on 25 Jul 2022
dinfo = dir('*.xls');
filenames = {dinfo.name};
numfiles = length(filenames);
all_row2 = [];
for K = 1 : numfiles
data = readtable(filenames{K});
if height(data) == 4
all_row2(end+1,:) = data(2,:);
end
end
Walter Roberson
on 9 Aug 2022
dinfo = dir('*.xls');
filenames = {dinfo.name}.';
numfiles = length(filenames);
all_row2 = [];
for K = 1 : numfiles
data = readtable(filenames{K});
if height(data) == 4
all_row2(end+1,:) = data(2,:);
end
end
output = [table(filenames), all_row2];
writetable(output, 'NameOfFileGoesHere.xlsx')
Walter Roberson
on 9 Aug 2022
dinfo = dir('*.xls');
filenames = {dinfo.name}.';
numfiles = length(filenames);
all_row2 = [];
for K = 1 : numfiles
data = readtable(filenames{K});
if height(data) == 4
if isempty(all_row2)
all_row2 = data(2,:);
else
all_row2(end+1,:) = data(2,:);
end
end
end
if isempty(all_row2)
error('No files with 4 rows found');
end
output = [table(filenames), all_row2];
writetable(output, 'NameOfFileGoesHere.xlsx')
Image Analyst
on 9 Aug 2022
Edited: Image Analyst
on 9 Aug 2022
Try this to extract the middle row of all .xls workbooks:
fileList = dir('data *.xls'); % Or *.xls* to get both .xls and .xlsx extensions.
allFileNames = {fileList.name}.';
numFiles = numel(allFileNames);
for k = 1 : numFiles
% Read in all the data.
data = readtable(allFileNames{k});
% Find out the number of rows.
rows = size(data, 1);
% Get the middle row of the table.
midRow = ceil(rows/2);
fprintf('Extracting the middle row (%d) from workbook %s.\n', midRow, allFileNames{k});
% Extract the middle row ONLY.
tMiddle = data(midRow, 2:end);
% Append middle row to our output table.
if k == 1
% Instantiate new table with same fieldnames.
tAllMiddleRows = tMiddle;
else
% Append to our growing table.
tAllMiddleRows = [tAllMiddleRows; tMiddle];
end
end
% Show the final output table values in the command window so we can verify them.
tAllMiddleRows
% Write table to disk as a new Excel .xlsx file.
% writetable(tAllMiddleRows, 'NameOfFileGoesHere.xlsx')
tAllMiddleRows =
2×5 table
Sample CSA_mm_2_ Minor_mm_ Major_mm_ Circularity
____________________________ _________ _________ _________ ___________
{'image_36003148480018.png'} 7.897 3.032 3.317 0.662
{'image_71793960610725.png'} 7.587 2.459 3.928 0.669
>>
Don't worry about the warnings. They're just saying that spaces in the column headers are converted into underlines because variable names can't have spaces in them. If you really want to suppress the warning, see attached file and figure it out.
mpz
on 11 Aug 2022
hi @Image Analyst I have a very similar question but having a hard time figuring out based on answers here. Question found at this link (https://www.mathworks.com/matlabcentral/answers/1777970-extract-specific-rows-and-columns-from-excel-and-store-in-a-matrix?s_tid=prof_contriblnk)
Random User
on 10 Oct 2022
Random User
on 10 Oct 2022
Image Analyst
on 10 Oct 2022
What does "retract" mean to you? To me it means to pull back or withdraw. Do you mean remove/delete or extract into a new vector) or something else?
Random User
on 10 Oct 2022
Image Analyst
on 10 Oct 2022
Still not sure what you mean. Please explain with an example in a new discussion thread. Please explain how the verb definition below applies to a matrix.
retract
1
[ ri-trakt ]
verb (used with object)
to draw back or in: to retract fangs.
verb (used without object)
to draw back within itself or oneself, fold up, or the like, or to be capable of doing this: The blade retracts.
Random User
on 10 Oct 2022
Random User
on 10 Oct 2022
Edited: Random User
on 10 Oct 2022
Categories
Find more on Data Import from MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!