Loading any Excel file from a folder
181 views (last 30 days)
Show older comments
Hey,
For a uni project i need to load any excel file (they all have different names) in my matlab function to analyse them later on.
But i dont know how to exactly load them and get them in my function. Here is what i got so far...
function test(path)
file_type = 'xlsx';
file_array = dir(fullfile(path,strcat('*.',file_type)));
file_folder = file_array(n_file).folder;
file_name = file_array(n_file).name;
file = strcat(file_folder,'\',file_name);
end
I am pretty new to matlab and would like to get some help from u guys. Thanks for helping me!!
1 Comment
Mathieu NOE
on 4 Feb 2021
hello
I guessed that you wanted to know how to import multiple files from a folder ?
this is one example :
range = 'C2:D10'; % Read a specific range of data:
file_list = dir('*.xlsx'); % list all xlsx files in current directory
for i = 1:length(file_list)
filename = file_list(i).name;
data_in{i} = xlsread(filename,range); % stores the filename data in array of cells
% insert your code here
end
this is another one
%% Read The processes 1 to l
% Specify the folder
myFolder1 = 'C:\Users\';
% Get a list of all files in the folder with the desired file name pattern.
filePattern1 = fullfile(myFolder1, '*.csv'); % Format/ pattern
theFiles1 = dir(filePattern1);
for l = 1 :1: length(theFiles1)
baseFileName1 = theFiles1(i).name;
fullFileName1 = fullfile(theFiles1(i).folder, baseFileName1);
fprintf(1, 'Now reading %s\n', fullFileName1);
[theFiles1(i).Data] = readtable(fullFileName1);
end
and more
% If you want to extract the content of all your .xls files within a certain directory at once, you can do it as :
% assuming you're already in the target directory
ds = spreadsheetDatastore(pwd, 'Sheets', 'cycle', 'Range', 'A1:C25', 'FileExtensions', '.xlsx', 'ReadVariableNames', true );
ds = readall(ds); % aggregates all contents as a single table
Accepted Answer
langrg
on 4 Feb 2021
Hi,
The code below should work.
function test(path)
% Find xlsx files
files = dir(fullfile(path, '*.xlsx'));
% Loop on every file found
for idx_file = 1:length(files)
% Read xlsx
[~, ~, rawData] = xlsread(fullfile(path, files(idx_file).name), 'Sheet1'); % 'Sheet1' or the name of the sheet to read
% Write your code after here
end
I don't know what you need to do after with data you loaded (rawData)...
3 Comments
Mathieu NOE
on 5 Feb 2021
hello
you can do this way :
% to select one file :
file = uigetfile('*.xlsx', 'Grab the file you want to process');
% to select multiple files :
file_list = uigetfile('*.xlsx', 'Grab the files you want to process', 'MultiSelect', 'on');
Mathieu NOE
on 5 Feb 2021
the code above will import the filename (one or multiple), then you can re use the rest of the code explained in earlier answers / comments
More Answers (1)
Benedikt Skurk
on 7 Feb 2021
1 Comment
Mathieu NOE
on 8 Feb 2021
hello
so I upgraded your code so that the file loading now works and you can display the data (with legend taken from the excel file headers)
you can decide with rows of data to display using the N vector in the code
[filename, pathname] = uigetfile('*.xlsx', 'Bitte Datei aussuchen');
[numericData, ~, rawData] = xlsread(fullfile(pathname, filename));
headers = rawData(1,:); % variable names
comments = rawData(2,:); % comments names / units
Y_legends= headers(2:end);
% replace underscore with blanks for legend strings
Y_legends2 = strrep(Y_legends,'_',' ');
[samples,nrows] = size(numericData);
y = numericData; % rows B and beyong (select rows with N if needed)
date_data = rawData(3:end,1);
N = 5:15; % channels of y data to plot
dn=datetime(date_data,'InputFormat','yyyy.MM.dd HH.mm.ss');
figure(1),plot(dn,y(:,N));
xlabel('Time (s)')
ylabel(' Auslastung in %');
legend(Y_legends2(N));
See Also
Categories
Find more on Spreadsheets in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!