MATLAB Answers

Jan
2

FAQ: How can I process a sequence of files?

Asked by Jan
on 26 Dec 2012
Latest activity Commented on by joshua Abam on 24 Jun 2019
Accepted Answer by Jan
How can I process a sequence of files?

  1 Comment

Jan
on 26 Dec 2012
This is a tutorial question only! There is no need to answer it and please to not vote for it. This is neither my question nor my answer, but only an example for a nicer, more convenient, more usable FAQ, which is less stuffed with commercials.

Sign in to comment.

3 Answers

Answer by Jan
on 26 Dec 2012
Edited by Jan
on 18 Dec 2018
 Accepted Answer

If the files that you want to process are sequentially numbered, like "file1.txt", "file2.txt", "file3.txt", etc. then you can use SPRINTF or NUM2STR to create the filename and LOAD, IMREAD, FOPEN, etc. to retrieve the data from the file. (Also note the three different ways of building the filename - you can use your favorite way.)
% Read files file1.txt through file20.txt, mat1.mat through mat20.mat
% and image1.jpg through image20.jpg. Files are in the current directory.
folder = cd;
for k = 1:20
matFilename = sprintf('mat%d.mat', k);
matData = load(fullfile(cd, matFilename));
jpgFilename = sprintf('image%d.jpg', k);
imageData = imread(jpgFilename);
textFilename = sprintf('file%d.txt', k);
fid = fopen(fullfile(folder, textFilename), 'rt');
textData = fread(fid);
fclose(fid);
end
In the above code, matData, imageData, and textData will get overwritten each time. You should save them to an array or cell array if you need to use them outside the loop, otherwise use them immediately inside the loop.
If instead you want to process all the files whose name matches a pattern in a directory, you can use DIR to select the files. Note that while this example uses *.jpg as the pattern and IMREAD to read in the data, as with the previous example you could use whatever pattern and file reading function suits your application's needs:
myFolder = 'C:\Documents and Settings\yourUserName\My Documents\My Pictures';
if exist(myFolder, 'dir') ~= 7
Message = sprintf('Error: The following folder does not exist:\n%s', myFolder);
uiwait(warndlg(Message));
return;
end
filePattern = fullfile(myFolder, '*.jpg');
jpegFiles = dir(filePattern);
for k = 1:length(jpegFiles)
baseFileName = jpegFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
fprintf('Now reading %s\n', fullFileName);
imageArray = imread(fullFileName);
imshow(imageArray); % Display image.
drawnow; % Force display to update immediately.
end
Or you can try a "File Exchange Pick of the Week": FileExchange: FileFun.
This answer is a modified version of:

  3 Comments

Hi
In follow up of your answer is it possible to apply same to iterations from Genetic algorithm, AS I am currently having challenges of saving the iterations from GA to a single file or each iteration to a seperate file
Thanks
Kind regards
Joshua
Each iteration in a separate file could result in hundreds of thousands of files.
persistent fid
if isempty(fid) || isempty(fopen(fid))
fid = fopen('output.txt','wt');
end
fprintf(fid, 'appropriate format', appropriate data) ;
Hi Walter
I seem not to have follow your suggestion can you help by adding some more comments to ease my grap or better still use this example to illustrate it and where best can I possible attached that to a GA code.
function f = gaintobj(x)
f = rastriginsfcn([x(1)-6 x(2)-13]);
f = f + rastriginsfcn([x(3)-3*pi x(4)-5*pi]);
lb = [1 1 -30 -30];
ub = [20 20 70 70];
%%
% Set the integer variables and number of variables.
IntCon = [1 2];
nvar = 4;
%%
% Set options to call the custom output function, and to initially have
% little crossover.
options = optimoptions('ga','OutputFcn',@gaoutputfunround,'CrossoverFraction',0.2);
%%
% For reproducibility, set the random number generator.
rng(10)
%%
% Set the objective function and call the solver.
fun = @gaintobj;
[x,fval] = ga(fun,nvar,[],[],[],[],lb,ub,[],IntCon,options)

Sign in to comment.


Answer by Sean de Wolski
on 31 May 2013

  3 Comments

Jan
on 31 May 2013
Somebody has accepted my answer. This has not been the intention, but of course the FAQ contain only answers worth to be accepted.
Was not me.

Sign in to comment.


Answer by Sean de Wolski
on 29 Jul 2016
Edited by Sean de Wolski
on 29 Jul 2016

In MATLAB R2014b and newer, you can use a datastore to read a sequence of files in a load-analyze-discard manner or in one shot with the readall() command.

  1 Comment

Hi all. Thanks for sharing it. How about if my files are in permutations sequence such as, Data11, Data12, Data 13, etc. Also, what if I would like to plot two different files names within a folder? Regards.

Sign in to comment.