Loading any Excel file from a folder

181 次查看(过去 30 天)
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 个评论
Mathieu NOE
Mathieu NOE 2021-2-4
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

请先登录,再进行评论。

采纳的回答

langrg
langrg 2021-2-4
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 个评论
Mathieu NOE
Mathieu NOE 2021-2-5
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
Mathieu NOE 2021-2-5
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

请先登录,再进行评论。

更多回答(1 个)

Benedikt Skurk
Benedikt Skurk 2021-2-7
Ok so my code is now:
function Auswertung(path)
file = uigetfile('*.xlsx', 'Bitte Datei aussuchen');
[~, ~, ~] = xlsread(fullfile(path, file(idx_file).name), 'Sheet1');
end
I can select a file but i am getting the error code: Not enough input arguments.
My goal after i selected my file is to read in each column individually starting at column B and take the values as y values and for each value take the associated time in the first column as an x value. Do u have any tips for that maybe? Sorry for all these questions!!
An example for an excel sheet is attached.
Best regards
  1 个评论
Mathieu NOE
Mathieu NOE 2021-2-8
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));

请先登录,再进行评论。

标签

Community Treasure Hunt

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

Start Hunting!

Translated by