Running 4 MATLAB Scripts to read, process and plot data from many csv files
Afficher commentaires plus anciens
Hi,
I have a number of folders (approx. 50) each with 3 csv files in them. At the moment I have to run the 4 scripts individually for one folder at a time as the name (and hence the path) of the folder is different. I would like to be able to run all these 4 scripts from potentially a 'master' script without editing the file path to each folder.
The names of the csv files within each folder are the same. For example, every folder has csv files called file1.csv, file2.csv and file3.csv. The csv file names do not differ from folder to folder.
The folders are named in a sequence for example, the first folder is AnBn, the next AnBn+1, AnBn+2, An+1Bn, An+1Bn+1, An+1Bn+2, An+2Bn, An+2Bn+1 ........ and so on where n is a number.
The code then saves plots to a path in each folder and exports data to a master Excel sheet, where each folder should have its own sheet within the Excel document.
To clarify, I would like to be able to run the scripts so they action every folder (the files within each folder) and save the plots that each folders data generates to that folder and that folders processed data to that folders own sheet within a master Excel document without stopping.
Is it possible to do what I am asking? I would really appreciate any help or advice.
Réponse acceptée
Plus de réponses (1)
Andrew Feenan
le 3 Juin 2022
9 commentaires
Mathieu NOE
le 3 Juin 2022
hello
sure you have to adapt to your xlsx file names structure
if you want to pick all files :
S = dir(fullfile(fileDir,'*.xlsx'));
if your files start always with blabla....xlsx , and you want only those : you can filter with
S = dir(fullfile(fileDir,'blabla*.xlsx'));
Andrew Feenan
le 3 Juin 2022
Andrew Feenan
le 4 Juin 2022
Mathieu NOE
le 7 Juin 2022
hello Andrew
I tested again the code and it works. It will look for your "data....xlsx" files inside all subfolders
make sure the matlab file is in the folder above the subfolders containing the data
for me :
yourpath = 'C:\Users\A0H36019\Documents'
and the data files are searched in the 11 subfolders :
dirnames = {'MATLAB'} {'Ma musique'} {'Mes images'} {'Mes vidéos'} {'Modèles Office …'} {'My Music'} {'My Pictures'} {'My Videos'} {'MyFolder'} {'cache'} {'users'}
NB : data files are not searched inside the main folder (yourpath)
if S is empty that means no data file (matching the file filter 'data_*.xlsx' is in the searched subfolder)
clc
clearvars
%% 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,'data_*.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
Andrew Feenan
le 7 Juin 2022
Andrew Feenan
le 7 Juin 2022
Mathieu NOE
le 8 Juin 2022
hello Andrew
so depending on which folder you have to specify the excel sheet ? this makes the code a bit more complicated...
maybe I would suggest that you create a cell array with folder names in the first column and associated (valid) excel sheet in the second column. of course you will have to "tweak" this portion of the code each time you will have another folder/subfolder structure with another / different sheet number associated with the data files.
another comment : I don't see what you want to achieve with the code :
filename1 = fullfile(fileDir, S(k).name);
filename2 = fullfile(fileDir, S(k).name);
the two lines are exactly the same... so both filenames are identical
the sheet number (or name) is a parameter you pass in the xlsread function :
data1 = xlsread(filename1 ,sheet1);
data2 = xlsread(filename2 ,sheet2);
Andrew Feenan
le 8 Juin 2022
Mathieu NOE
le 9 Juin 2022
Hi andrew
OK - good news
Catégories
En savoir plus sur Downloads dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
