Info
Cette question est clôturée. Rouvrir pour modifier ou répondre.
How to retrieve an extension type based on the location of a folder that is storing images?
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaire
Greg
le 21 Juin 2018
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.
Réponses (1)
Greg
le 21 Juin 2018
Modifié(e) : Greg
le 21 Juin 2018
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 commentaire
Greg
le 21 Juin 2018
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.
Cette question est clôturée.
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!