running a script in different folders

7 次查看(过去 30 天)
Hi all,
I have been working on a piece of code that will automatically process multiple files in a folder automatically
saveFolder=''%save folder for data
FolderLength = ''
File_Struct = dir(fullfile(FolderLength, '*.mat'))
File_Path = cell(size(File_Struct,1), 1);
for k = 1 : 1 : size (File_Struct,1)
File_Path {k} = fullfile(File_Struct(k).folder, File_Struct(k).name);
fprintf(1, 'Now reading %s\n', File_Path {k});
load(File_Path {k}); %this is changed
Data1 = ProcessData1(Data);
Data2 = ProcessData2(Data);
save(fullfile(saveFolder, sprintf('psData_%d.mat', k)),'Data')
I have a folder which has 3 subfolders, each has 10 files. The code above is running the functions on each subfolder indivisually (10 files).
I know that
'**/*.mat'
will process all files in a main folder, but I want to define the subfolders and corresponding save folders somehow, then ask matlab to go over each subfolder --> process data using the above code--> then save to 1 of the 3 save folders.
I tried to use different methods like changing the directory or definining the multiple subfolder like:
mainfolder={'path for 1st subfolder','path for 2nd subfolder','path for 3rd subfolder'}
for i=mainfolder
cd=(mainfolder{1})
saveFolder=''%save folder for data
FolderLength = ''
File_Struct = dir(fullfile(FolderLength, '*.mat'))
File_Path = cell(size(File_Struct,1), 1);
for k = 1 : 1 : size (File_Struct,1)
File_Path {k} = fullfile(File_Struct(k).folder, File_Struct(k).name);
fprintf(1, 'Now reading %s\n', File_Path {k});
load(File_Path {k}); %this is changed
Data1 = ProcessData1(Data);
Data2 = ProcessData2(Data);
save(fullfile(saveFolder, sprintf('psData_%d.mat', k)),'Data')
cd=(mainfolder{2})
saveFolder2=''%save folder for data
Data1 = ProcessData1(Data);
Data2 = ProcessData2(Data);
save(fullfile(saveFolder2, sprintf('psData_%d.mat', k)),'Data')
cd=(mainfolder{3})
saveFolder3=''%save folder for data
Data1 = ProcessData1(Data);
Data2 = ProcessData2(Data);
save(fullfile(saveFolder3, sprintf('psData_%d.mat', k)),'Data')
end
end
, but it did not work. Any hint would be appreciated

回答(1 个)

Image Analyst
Image Analyst 2020-11-10
Use sprintf() inside the loop to create a specified saveFolder. Something like
Try this code, which has a number of improvements to improve robustness:
inputFolder = pwd % Wherever...
filePattern = fullfile(inputFolder, '*.mat')
File_Struct = dir(filePattern)
numberOfFiles = length(File_Struct);
for k = 1 : numberOfFiles
thisFolder = File_Struct(k).folder;
fullInputFileName = fullfile(thisFolder, File_Struct(k).name);
fprintf('%4d of %4d : Now reading input %s\n', k, numberOfFiles, fullInputFileName);
str = load(fullInputFileName);
if isfield(str, 'Data')
Data = str.Data;
Data1 = ProcessData1(Data);
Data2 = ProcessData2(Data);
% Make up the sub-folder name somehow. Adapt as needed to make it whatever it needs to be.
newSubFolder = sprintf('%d', k);
outputFolder = fullfile(inputFolder, newSubFolder);
if ~isfolder(outputFolder)
% Make output folder if it does not exist.
mkdir(outputFolder);
end
% Now that we have the subfolder name, make up the new base file name of the mat file.
baseFileName = sprintf('psData_%d Data', k);
fullOutputFileName = fullfile(outputFolder, baseFileName);
fprintf(' Saving output %s.\n', fullOutputFileName);
% Save Data into a new mat file.
save(fullOutputFileName, 'Data')
else
fprintf(' !!!! Data variable not found inSaving output %s.\n', fullInputFileName);
end
end
Adapt as needed.

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by