Importing Excel from MATLAB

조회 수: 5 (최근 30일)
Tati Pati
Tati Pati 2021년 12월 3일
댓글: Mathieu NOE 2021년 12월 7일
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
Walter Roberson
Walter Roberson 2021년 12월 3일
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 2021년 12월 3일
Thank you. It's is the former. There is exactly on xlsx file per subdirectory that has common prefix with the others.

댓글을 달려면 로그인하십시오.

답변 (1개)

Mathieu NOE
Mathieu NOE 2021년 12월 3일
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
  댓글 수: 1
Mathieu NOE
Mathieu NOE 2021년 12월 7일
hello
problem solved ?

댓글을 달려면 로그인하십시오.

카테고리

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