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

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 Comment

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.

Answers (1)

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 Comment

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.

This question is closed.

Asked:

on 20 Jun 2018

Closed:

on 20 Aug 2021

Community Treasure Hunt

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

Start Hunting!