Running 4 MATLAB Scripts to read, process and plot data from many csv files
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Andrew Feenan
am 3 Jun. 2022
Kommentiert: Mathieu NOE
am 9 Jun. 2022
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.
0 Kommentare
Akzeptierte Antwort
Mathieu NOE
am 3 Jun. 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
0 Kommentare
Weitere Antworten (1)
Siehe auch
Kategorien
Mehr zu File Operations finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!