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.
