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
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개)
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
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!