Importing Excel from MATLAB

Hello experts I have three directories
'directorybase1', 'directorybase2', 'directorybase3'
Inside each of these directions there is respective excel file Sheetal.xlsx, Sheeta2.xlsx, Sheeta3xlsx.
Notice that the names of the directories and the Sheets within have commonality..
How can I using Matlab
1- pull out these three excel files? 2- read them and stored the somewhere the data in seperate variables
I am looking for help specifically in a way to generalize in case there are many folders and not only 3
Thank you very much

3 commentaires

KALYAN ACHARJYA
KALYAN ACHARJYA le 3 Déc 2021
There are many answers to similar questions, please do a search. You can also see the answer in call multiple images (just need to change the file extension)
Walter Roberson
Walter Roberson le 3 Déc 2021
Is there exactly one xlsx file per subdirectory? Is there exactly one xlsx file that has a common prefix for them all, with the other xlsx files in the subdirectories certain to have a different prefix? Or is it important to be able to pull out the trailing number from the directory name and use it to find the corresponding sheet name because there are other files with similar names in the subdirectories ?
Tati Pati
Tati Pati le 3 Déc 2021
Thank you. It's is the former. There is exactly on xlsx file per subdirectory that has common prefix with the others.

Connectez-vous pour commenter.

Réponses (1)

Mathieu NOE
Mathieu NOE le 3 Déc 2021

0 votes

hello
my 2 cents suggestion for looping over folders and files
this can be further refined if you need an additionnal loop to retrieve multiple sheets from the excel files.
I use this excellent FEX submission to make sure the files are loaded accordingly to their natural name sorting (what matlab does not by default)
clc
clearvars
%% define path
yourpath = pwd; % or your specific path
list=dir(pwd); %get info of files/folders in current directory
isfile=~[list.isdir]; %determine index of files vs folders
dirnames={list([list.isdir]).name};
dirnames=dirnames(~(strcmp('.',dirnames)|strcmp('..',dirnames)));
%%
sheet = 1; % specify which sheet to be processed (my demo) - if needed
%% Loop on each folder
for ci = 1:length(dirnames) %
fileDir = char(dirnames(ci));
S = dir(fullfile(fileDir,'Sheeta*.xlsx')); % get list of data files in directory
S = natsortfiles(S); % sort file names into natural order (what matlab does not) , see FEX :
%(https://fr.mathworks.com/matlabcentral/fileexchange/47434-natural-order-filename-sort)
%% Loop inside folder
for k = 1:length(S) % read data in specified sheet
data = xlsread(fullfile(fileDir, S(k).name),sheet); % or use a structure (S(k).data ) to store the full data structure
% your own code here for data processing. this is just for my demo
% for now :
title_str = [fileDir ' / ' S(k).name ' / sheet : ' num2str(sheet)];
figure,plot(data),title(title_str);
end
end

Catégories

Question posée :

le 3 Déc 2021

Commenté :

le 7 Déc 2021

Community Treasure Hunt

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

Start Hunting!

Translated by