Read multiple excel sheets and read a particular column from each and list them in a separate Excel file

2 visualizaciones (últimos 30 días)
I have 10 excel sheets with similar structure (same column and row headings, but with different readings). I need to read the excel files and extract one column from each sheet and save into another separate excel sheet.
Eg.
Sl. No. Name Date
1 xxx yyy
2 xxx yyy
If this is the structure of the input sheets, I need only the Name column saved in the output sheet as seen below;
Name Name Name
xxx xxx xxx
xxx xxx xxx
I used this to open multiple excel files: [file,filepath]=uigetfile({'*.csv;*.xlsx;*.xls';},'Select Trajectory Table File(s)', 'MultiSelect', 'on');

Respuesta aceptada

Ameer Hamza
Ameer Hamza el 19 de Mzo. de 2020
Editada: Ameer Hamza el 20 de Mzo. de 2020
Try something like this
[file,filepath]=uigetfile({'*.csv;*.xlsx;*.xls';},'Select Trajectory Table File(s)', 'MultiSelect', 'on');
output_file_name = 'output_file.xlsx';
column_name = {'T3x'}; % <--- column name here
for i=1:numel(file)
t = readtable([filepath file{i}]);
writetable(table(t.(column_name{:}), 'VariableNames', column_name), output_file_name, 'Range', [char(64+i) '1'])
end
t.Name in last line of for loop specify which column is selected from the original files.
For multiple columns, the following code will work
[file,filepath]=uigetfile({'*.csv;*.xlsx;*.xls';},'Select Trajectory Table File(s)', 'MultiSelect', 'on');
output_file_name = 'output_file.xlsx';
column_name = {'Hx', 'Hy'}; % <--- columns name here
for i=1:numel(file)
t = readtable([filepath file{i}]);
writetable(t(:, ismember(t.Properties.VariableNames, column_name)), output_file_name, 'Range', [char(65+numel(column_name)*(i-1)) '1'])
end
  22 comentarios
Wayne
Wayne el 20 de Mzo. de 2020
Is there any alternative to the solution that you gave yesterday for reading only single input file rather than multiple files without the string error.
Ameer Hamza
Ameer Hamza el 20 de Mzo. de 2020
Here is a quick fix. Just change the beginning of the code like this
[file,filepath]=uigetfile({'*.csv;*.xlsx;*.xls';},'Select Trajectory Table File(s)', 'MultiSelect', 'on');
if ~iscell(file)
file = {file};
end
output_file_name = 'output_file.xlsx';

Iniciar sesión para comentar.

Más respuestas (0)

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by