How do I access images from directories using an input command for the extensive file?

4 次查看(过去 30 天)
I have a function which accesses images from a directory using inputs dirname and the filetype. When I type filenames = image(dirname,filetype), (e.g. filenames = image('/Users/Macbookair','jpg'), I can access all the files from the directory if the code has the line fileIdentity = fullfile(dirname, '*.jpg').
However, if I type in filenames = image('/Users/Macbookair','png'), the folders of jpg still appear in the command window. My question is how do I add an input command, such as filetype, so that whenever I type 'png', 'jpg' or any other extensive file it will show up as that specific file.
Thank you!
  2 个评论
Walter Roberson
Walter Roberson 2019-9-6
Why did you name your function "image", when that is the name of a key matlab function for drawing images? imshow() and imagesc() also internally depend upon image() so by naming your function "image" you make it quite difficult to draw images.
Walter Roberson
Walter Roberson 2019-9-6
We do not know how your existing function accesses file names, which makes it difficult to know what to suggest.

请先登录,再进行评论。

采纳的回答

Neuropragmatist
Neuropragmatist 2019-9-6
In the example function you gave the second input 'fileType' is not used, is that the problem?
You could use this in the line calling fullfile to get only the filetypes you want, i.e.:
function [filenames] = GenerateImageList(dirname, fileType)
fileIdentity = fullfile(dirname, ['*' fileType]);
files = dir(fileIdentity);
for i = 1:length(files)
filenames = files(i).name;
fprintf('%s\n', filenames);
end
end
For example you can run this code as:
filenames = GenerateImageList(pwd,'png')
% or
filenames = GenerateImageList(pwd,'jpg')
However, there are some problems with your code - if there are no files with the extension you ask for the function throws an error because filenames is left undefined. Also, I think you want filenames to contain a list of all the filenames? But the way it is written here it will only ever contain the last filename it found because you overwite it on every loop. Might I suggest something like this:
function [filenames] = GenerateImageList(dirname, fileType)
fileIdentity = fullfile(dirname, ['*' fileType]);
files = dir(fileIdentity);
filenames = {files(:).name};
fprintf(1, '%s\n', filenames{:})
end
If you don't need to display the text in the command window for every filename (why do you want that?) you can just call dir directly for the filename list:
function [filenames] = GenerateImageList(dirname, fileType)
df = dir(fullfile(dirname, ['*' fileType]));
filenames = {df(:).name};
end
And then maybe you don't need the GenerateImageList function at all, you could just put those lines of code directly inb the parent function...
Hope this helps,
M.
  7 个评论

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by