Question

I want to read .PGM files from specific folder.

Here is my specific folder that keep all the .pgm files

  a = 'D:\Matlab\Training\Training_PGM_All\';

And I try to read all the files inside that folder

        tmpdir =  dir([a, '*']);

I still cannot read all those files. I dont want to specific the path directly in the code. So I want to keep the directory in a variable and then I'll call that variable in the code.

What's wrong with that code..

Was it helpful?

Solution

I don't see the problem. Works fine for me :)

a = 'D:\Matlab\Training\Training_PGM_All\';
Files=dir(a);
for k=1:length(Files)
   FileNames=Files(k).name
end

OTHER TIPS

Reading all files in a directory is covered in the MATLAB FAQ, actually. One example given is

% 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 = strcat('image', num2str(k), '.jpg');
  imageData = imread(jpgFilename);
  textFilename = ['file' num2str(k) '.txt'];
  fid = fopen(textFilename, 'rt');
  textData = fread(fid);
  fclose(fid);
end

Another example is

myFolder = 'C:\Documents and Settings\yourUserName\My Documents\My Pictures';
if ~isdir(myFolder)
  errorMessage = sprintf('Error: The following folder does not exist:\n%s', myFolder);
  uiwait(warndlg(errorMessage));
  return;
end
filePattern = fullfile(myFolder, '*.jpg');
jpegFiles = dir(filePattern);
for k = 1:length(jpegFiles)
  baseFileName = jpegFiles(k).name;
  fullFileName = fullfile(myFolder, baseFileName);
  fprintf(1, 'Now reading %s\n', fullFileName);
  imageArray = imread(fullFileName);
  imshow(imageArray);  % Display image.
  drawnow; % Force display to update immediately.
end

Source: http://matlab.wikia.com/wiki/FAQ

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top