running a script in different folders

조회 수: 7 (최근 30일)
maryam al labbad
maryam al labbad 2020년 11월 10일
답변: Image Analyst 2020년 11월 10일
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