MATLAB: Importing data from different excel sheets and many excel files (csv files)

excel sheet many excel files

Hi everyone, I am looking for a help:) I obtained data from Imaris analysis and now I've got around 70 excel files. Each contain many sheets (which contain a values for different parameter). I want to create variables for each parameter and combine values from this 70 excel files. Subsequently I want to do some calculations (mean value etc)
I ve got something like this:
clear all
close all
matfiles = dir('blablabla\*.csv');
% And here should be something for excel sheet:)
branches = []
junctions =[]
for i=1:length(matfiles)
Z = csvread(sprintf('%i.csv',(i)),1,1);
branches = Z(:,1);
junctions = Z(:,2);
mean_branches (i) = sum(branches);
mean_junctions (i) = sum(junctions);
end
Thank you in advance:)

Best Answer

Hi CoCo,
You can try this below. I made some comments also.
fPath = 'C:\Users\...\Desktop\excelsFolder'; % filepath of your excel or csv files
cd(fPath) % you don't need to change directory you can use dir with filepath also.
fNames = dir('*.xlsx'); % I used 4 excels named: excel1.xlsx,excel2.xlsx,excel3.xlsx,excel4.xlsx ...
fileNamesArray = cell(1,numel(fNames)); % get the excel file names
j = 1; % define another counter for inner for loop.
for i = 1:numel(fNames)
fileNamesArray{i} = fNames(i).name; % gets the file names in the directory you can also work another directory too.
[excelStatus,excelSheets] = xlsfinfo(fNames(i).name); % gives you the sheet list (not sure if it works with csv files also)
for k = 1:numel(excelSheets) % loop for each sheet in excel
[num,txt,raw] = xlsread(fNames(i).name,excelSheets{k}); % reads the k.th sheet and gets data into raw. you can also use csvread function which reads only numeric values.
% get what you need from here from the num or raw value store it inside
% any other array and continue your life like below.
myStoredData{j,1} = raw(:,1); % store your i.th excel's k.th sheet data in myStoredData cell array. Or you can use numeric array
j = j+1;
end
end