Load different files from folders
Ältere Kommentare anzeigen
Hi, I have one folder which contains data from patients. Every patient has its own subfolder with 3 files in it. I wnat to load a .csv and .xmp file from it.
I have thought of using a for-loop to extract the data, but dont know how to implement it, because I want to load all data and save it. At the end of my script I want to do a LOOCV, so thats why I am needing all data sets.
thanks in advance
2 Kommentare
Mathieu NOE
am 17 Okt. 2022
hello
you can try build your code based on the example below
It must be somehow adapted to your own specific case though
%% define path
yourpath = pwd; % or your specific path
list=dir(yourpath); %get info of files/folders in current directory
isfile=~[list.isdir]; %determine index of files vs folders
dirnames={list([list.isdir]).name}; % directories names (including . and ..)
dirnames=dirnames(~(strcmp('.',dirnames)|strcmp('..',dirnames))); % remove . and .. directories names from list
%% demo for excel files
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)); % current directory name
S = dir(fullfile(fileDir,'Sheeta*.xlsx')); % get list of data files in directory according to name structure 'Sheeta*.xlsx'
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
Akzeptierte Antwort
Weitere Antworten (0)
Kategorien
Mehr zu Audio and Video Data finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!