Load different files from folders
3 views (last 30 days)
Show older comments
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 Comments
Mathieu NOE
on 17 Oct 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
Accepted Answer
Jan
on 17 Oct 2022
Do the .xmp files have the same name as the .csv files? Do all .csv files inside the base folder belong to the measurements? Then:
BaseFolder = 'C:\Your\Basefolder';
CSVList = dir(fullfile(BaseFolder, '**', '*.csv'));
nFile = numel(CSVList);
data = cell(1, nFile);
for k = 1:nFile
csv = fullfile(CSVList(k).folder, CSVList(k).name);
... import the CSV, e.g. data{k} = csvread(csv)
[~, name] = fileparts(CSVList(k).name);
xmp = fullfile(CSVList(k).folder, [name, '.xmp']);
... import XMP
end
Do not expect readers to be experts in your field of science. I do not know, what a "LOOCV" is, but I assume, this does not matter for solving the problem of the question.
0 Comments
More Answers (0)
See Also
Categories
Find more on Audio and Video Data 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!