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

조회 수: 6 (최근 30일)
Coco
Coco 2019년 1월 3일
댓글: Luna 2019년 1월 4일
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:)

채택된 답변

Luna
Luna 2019년 1월 3일
편집: madhan ravi 2019년 1월 4일
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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by