You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
Extracting specific data from multiple excel files to create a single excel file on MAC
6 views (last 30 days)
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
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 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
Hi Image Anlayst, I was wondering if I need to retract a row from the column CSA with the minmum value how could I do this. I tried to use the min function however I am finding it difficult to retract the column and row number to store this to then use it to retract that row with the lowest CSA value. Thank you once again :)
Random User
on 10 Oct 2022
CSA_cols = data(:,3);
minimum_val = min(CSA_cols);
[rows, cols] = find(data ==minimum_val);
I tried this however there was an error of using min. invalid data type. first argument must be numerical or logical
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
In the intial question I was retracting the middle row of each excel speadsheet, now instead of the middle row I want to take out the row with the lowest CSA value. Then save this in an excel spreadsheet.
I am finding it hard to find the minimum value in the CSA through code.
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
Sorry to clarify - what I mean is to take out not to draw back.
In the orginal question we took out the middle row and now instead of the middle row I would like to retrieve the row with the lowest CSA and combining this for all the files in another excel spreadsheet.
When we open data 1. these are the headings
Sample CSA (mm^2) Minor (mm) Major (mm) Circularity
for the third column CSA I would like to find the minimum value and then save it into another excel file. In the same manner that we answered this question.
See Also
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!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)