Load different files from folders
조회 수: 2 (최근 30일)
이전 댓글 표시
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
Mathieu NOE
2022년 10월 17일
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
채택된 답변
Jan
2022년 10월 17일
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
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Audio and Video Data에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!