MATLAB: How to reformat data from multiple files in a folder

for loopMATLABwhile loop

I have written code to open a command window allowing the user to choose a folder. Then The files from this folder will be opened. I have this part of the code working. Now I need to include a code I had previously written to reformat the data in each file.
myFolder = uigetdir('C:\Users\c13459232\Documents\MATLAB');
if ~isdir(myFolder)
errorMessage = sprintf('Error: the following folder does not exist: \n%s', myFolder);
uiwait(warndlg(errorMessage));
return;
end
filePattern = fullfile(myFolder, '*.asc');
Files = dir(filePattern);
for k = 1 : length(Files)
baseFileName = Files(k).name;
FileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now reading %s\n', FileName);
This is the first section of the code I have written to open the files
Cell = textscan( FileName, '%d', 'delimiter', ';');
Data = cell2mat(Cell);
N = 1024;
Finish = reshape(Data, N, [])';
end
This code reformats the data the way I want it. I know I will need to write the code to create 50 new files (e.g. Finish_01, Finish_02, etc). I'm not sure how to compose this. Any tips would be greatly appreciated.

Best Answer

Before going to the generation of the new files, let's fix a detail with the reading. I don't know if it is working for you, but the textscan function expects a file identifier or a string. In your case, you want to read a file, so you should use a file identifier, I mean, you should replace your line:
Cell = textscan( FileName, '%d', 'delimiter', ';');
with the following 3 lines:
fid = fopen(FileName);
Cell = textscan( fid, '%d', 'delimiter', ';');
fclose(fid);
Now, for the matter of saving several files in an output folder, you should add this before the for loop:
outFolder = fullfile(myFolder, 'output');
mkdir(outFolder);
then use that created folder for saving files. For that you should add these 3 lines as the last lines of your for loop:
newFileName = fullfile(outFolder, ['finish_' sprintf('%03d', k) '.asc']); % finish_001.asc, finish_002.asc, ...

dlmwrite(newFileName, Finish, ';');
disp([':: Now writing ' newFileName]);
The full (merged) code would be:
myFolder = uigetdir('C:\Users\c13459232\Documents\MATLAB');
if ~isdir(myFolder)
errorMessage = sprintf('Error: the following folder does not exist: \n%s', myFolder);
uiwait(warndlg(errorMessage));
return;
end
outFolder = fullfile(myFolder, 'output');
mkdir(outFolder);
filePattern = fullfile(myFolder, '*.asc');
Files = dir(filePattern);
for k = 1 : length(Files)
baseFileName = Files(k).name;
FileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now reading %s\n', FileName);
fid = fopen(FileName);
Cell = textscan( fid, '%d', 'delimiter', ';');
fclose(fid);
Data = cell2mat(Cell);
N = 1024;
Finish = reshape(Data, N, [])';
newFileName = fullfile(outFolder, ['finish_' sprintf('%03d', k) '.asc']); % finish_001.asc, finish_002.asc, ...
dlmwrite(newFileName, Finish, ';');
disp([':: Now writing ' newFileName]);
end
I'm attaching a code compare image, your code on Left, the new code on Right, so you can easily view the differences:
Hope this help,
Frank