Info

此问题已关闭。 请重新打开它进行编辑或回答。

How to retrieve an extension type based on the location of a folder that is storing images?

1 次查看(过去 30 天)
I am writing a code that takes in images, allows a user to create a new folder with a user defined spacing between images. When I prompt the user for the location of the images eg. C:\Users\Joe\Desktop\2018523\Images, I would like to be able to retrieve the extension type of the images.
f= figure;
whatpath = 'Where are your unindexed images?';
BackSlash = '\';
star = '*';
wdinput = uicontrol(f,'style', 'pushbutton','string', whatpath,
'position', [100,190,360,20],
'callback','whatpath = uigetdir;[filepath1,whatdir,ext]=fileparts(whatpath);
whatdirectory =strcat(filepath1,BackSlash,whatdir,BackSlash,star,T);set(gcbo,''String'',whatpath)');
So far the ext variable, from [filepath1,whatdir,ext]=fileparts(whatpath), is a 0x0 Char. There are 700+ .tif files in the Images folder. I would like it to automatically grab extension type so that the user doesn't have to specify that as well.
  1 个评论
Greg
Greg 2018-6-21
Replace
strcat(filepath1,BackSlash,whatdir,BackSlash,star,T)
with
fullfile(filepath1,whatdir,[star,T]);
Also, what is "T"? I'm not seeing it in your posted code.

回答(1 个)

Greg
Greg 2018-6-21
编辑:Greg 2018-6-21
The ext has to be empty, you've used uigetdir and directories can't have extensions. Your best bet is do a quick dir on the returned path and see which extensions are in the directory.
allfiles = dir(whatdirectory);
filenames = {allfiles.name}';
[~,~,exts] = cellfun(@fileparts,filenames,'UniformOutput',false);
exts = unique(exts);
Alternatively, if you mandate that all files share an extension type, and at least one exists in the folder before running the code, you could use uigetfile instead of uigetdir:
[whatfile,whatpath] = uigetfile('*.*');
[~,~,ext]=fileparts(whatfile);
  1 个评论
Greg
Greg 2018-6-21
Actually "directories can't have extensions" isn't necessarily true from the perspective of using fileparts. To my knowledge, it does a simple lookup of the last period after the last filesep character. So a directory including a period in its name would return an ext output.

此问题已关闭。

Community Treasure Hunt

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

Start Hunting!

Translated by