MATLAB Answers

Jan Simon
0

FAQ: How can I process a sequence of files?

Asked by Jan Simon
on 26 Dec 2012
Latest activity Commented on by Walter Roberson
about 13 hours ago

How can I process a sequence of files?

  1 Comment

Jan Simon
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.

See http://www.mathworks.com/matlabcentral/answers/57443-professional-looking-matlab-faq.

Products

No products are associated with this question.

2 Answers

Answer by Jan Simon
on 26 Dec 2012
Edited by Jan Simon
on 19 Mar 2016
 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.
for k = 1:20
  matFilename  = sprintf('mat%d.mat', k);
  matData      = load(matFilename);
  jpgFilename  = sprintf('image%d.jpg', k);
  imageData    = imread(jpgFilename);
  textFilename = sprintf('file%d.txt', k);
  fid          = fopen(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:

matlab.wikia.com/wiki/FAQ: How_can_I_process_a_sequence_of_files   [EDITED, link fixed by I.A.]

  3 Comments

Hello Mr Simon,

I used the idea provided by you to load multiple files where each file has x, y coordinates and u v (velocity components). For each file, it was easier to plot vectors using quiver. But i want an animation where the figure remains the same and each time a file is loaded, vectors are plotted for it then the next file - its vectors and so on till the last file. The idea is to get an animation. Kindly help Thanks

Bineet_Mehra please open a new Question for that.


Answer by Sean de Wolski
on 31 May 2013

  3 Comments

Jan Simon
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.

Nor me.


Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!