Info

この質問は閉じられています。 編集または回答するには再度開いてください。

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

1 回表示 (過去 30 日間)
Caitlin Taylor
Caitlin Taylor 2018 年 6 月 20 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
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