Running 4 MATLAB Scripts to read, process and plot data from many csv files

7 vues (au cours des 30 derniers jours)
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

Mathieu NOE
Mathieu NOE le 3 Juin 2022
hello
see example below
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,'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

Plus de réponses (1)

Andrew Feenan
Andrew Feenan le 3 Juin 2022
Thank you for your help.
When I run the this for loop S is empty. What should i replace Sheeta*.xlsx with? The file name I use does not work.
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)
  9 commentaires
Andrew Feenan
Andrew Feenan le 8 Juin 2022
Hi Mathieu,
filename1 = fullfile(fileDir, 'data1.csv');
filename2 = fullfile(fileDir, 'data2.csv');
V0 = readtable(filename1,'Headerlines',543);
g = readcell(filename2,'Range','L78');
This is what I used and it's working fine now.
Thank you for your help.
Andrew
Mathieu NOE
Mathieu NOE le 9 Juin 2022
Hi andrew
OK - good news

Connectez-vous pour commenter.

Catégories

En savoir plus sur Get Started with MATLAB dans Help Center et File Exchange

Produits


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by