我想从特定文件夹中读取.PGM文件。
这是我保留所有.pgm文件的特定文件夹
a = 'D:\Matlab\Training\Training_PGM_All\';
我尝试读取该文件夹中的所有文件
tmpdir = dir([a, '*']);
我仍然无法读取所有这些文件。我不想直接在代码中具体说明路径。所以我想将目录保存在变量中,然后我将在代码中调用该变量。
该代码有什么问题..
答案 0 :(得分:0)
我没有看到问题所在。适合我的工作:)
a = 'D:\Matlab\Training\Training_PGM_All\';
Files=dir(a);
for k=1:length(Files)
FileNames=Files(k).name
end
答案 1 :(得分:0)
实际上,读取目录中的所有文件都在MATLAB FAQ中介绍。给出的一个例子是
% 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
另一个例子是
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