Read multiple excel files and get the maximum value

I have a folder with 960 excel file each with 1 sheet - 3 columns and 96000 odd rows. I need to find the maximum absolute value of each column and save in another excel. I tried the following
files = dir('*.csv');
for i = 1:length(files)
data = xlsread(files(i).name);
max2010(i) = max(data);
end
and obtained this error
Unable to perform assignment because the left and right sides have a different number of elements.
Error in Maxvalue (line 4)
max2010(i) = max(data); % Maximum value in entire dataset

Respuestas (1)

KSSV
KSSV el 5 de Mayo de 2022
Editada: KSSV el 5 de Mayo de 2022
files = dir('*.csv');
N = length(files) ; % initialize the required maximum array
max2010 = zeros(N,3) ;
for i = 1:length(files)
data = xlsread(files(i).name); % read the file
max2010(i,:) = max(data); % Get the max and save in array
end

4 comentarios

I tried the code . After reading file number 4, the following error appears
Error using xlsread (line 257)
Excel Worksheet could not be activated.
Error in Maxvalue (line 5)
data = xlsread(files(i).name); % read the file
KSSV
KSSV el 5 de Mayo de 2022
Use readtable instead of xlsread.
Warning: Column headers from the file were modified to make them valid MATLAB identifiers before
creating variable names for the table. The original column headers are saved in the
VariableDescriptions property.
Set 'VariableNamingRule' to 'preserve' to use the original column headers as table variable names.
Error using max
Invalid data type. First argument must be numeric or logical.
Error in Maxvalue (line 6)
max2010(i,:) = max(data); % Get the max and save in array
You cannot use straight away max on table. You need to follow as below:
files = dir('*.csv');
N = length(files) ; % initialize the required maximum array
max2010 = zeros(N,3) ;
for i = 1:length(files)
data = readtable(files(i).name); % read the file into a table
data = table2array(data) ; % convert table to array
max2010(i,:) = max(data); % Get the max and save in array
end

Iniciar sesión para comentar.

Categorías

Más información sobre Data Import from MATLAB en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 5 de Mayo de 2022

Comentada:

el 6 de Mayo de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by