Extract number and infromation from multiple image files ?

2 ビュー (過去 30 日間)
D. Frank
D. Frank 2020 年 6 月 25 日
コメント済み: D. Frank 2020 年 7 月 7 日
Hi everybody
I have a folder with many images in a structure of name: "name_id_324_988_true_207_1.6", i want to take the part "324_988_true" to calculate, for example:
ls Defolder;
23l9p9_Sy3lMlT_265_332_false_270_1.32.jpg
38bvq6_dwJCL2R_126_665_true_6_0.5.jpg
i want to take the part "265_332_false" for each image and show the calculated number on the distribution table for example:
folder = 'Defolder';
S = dir(fullfile(folder,'*.jpg'));
N = {S.name};
[~,F] = cellfun(@fileparts,N,'uni',0);
V = str2double(F);
V1 = V';
all i got is a lot of NaN, please help, thank youu
  1 件のコメント
Rik
Rik 2020 年 6 月 25 日
Try strsplit with underscore as delimiter.

サインインしてコメントする。

採用された回答

Rik
Rik 2020 年 6 月 27 日
The code below should do what you need. Adapt as needed.
folder = 'Defolder';
S = dir(fullfile(folder,'*.jpg'));
N = {S.name};
%overwrite N so it works on my computer as well
N={'23l9p9_Sy3lMlT_265_332_false_270_1.32.jpg','38bvq6_dwJCL2R_126_665_true_6_0.5.jpg'};
c=cellfun(@MyFun,N,'UniformOutput',false);
celldisp(c)
function c=MyFun(str)
c=strsplit(str,'_');
c=c(3:5);
c{1}=str2double(c{1});
c{2}=str2double(c{2});
c{3}=strcmp(c{3},'true');
end
  12 件のコメント
Rik
Rik 2020 年 7 月 7 日
Leave this function intact:
function c=MyFun(str)
c=strsplit(str,'_');
c=c(3:5);
c{1}=str2double(c{1});
c{2}=str2double(c{2});
c{3}=strcmp(c{3},'true');
c{4}=(c{1}/45)*c{2};
end
Then you can modify the code after cellfun:
folder = 'Defolder';
S = dir(fullfile(folder,'*.jpg'));
N = {S.name};
%overwrite N so it works on my computer as well
N={'23l9p9_Sy3lMlT_265_332_false_270_1.32.jpg','38bvq6_dwJCL2R_126_665_true_6_0.5.jpg'};
c=cellfun(@MyFun,N,'UniformOutput',false);
c=vertcat(c{:});
for n=1:size(c,2)
c{1,n}=cell2mat(c(:,n));
end
c(2:end,:)=[];
%you can also put more descriptive variable names here instead
plot(c{1},c{2},'*')
function c=MyFun(str)
c=strsplit(str,'_');
c=c(3:5);
c{1}=str2double(c{1});
c{2}=str2double(c{2});
c{3}=strcmp(c{3},'true');
c{4}=(c{1}/45)*c{2};
end
D. Frank
D. Frank 2020 年 7 月 7 日
i got it, thank you!

サインインしてコメントする。

その他の回答 (1 件)

Stephen23
Stephen23 2020 年 7 月 1 日
One simple regular expression does this quite efficiently:
D = 'Defolder';
S = dir(fullfile(D,'*.jpg'));
N = {S.name};
T = regexpi(N,'(\d+)_(\d+)_(true|false)','tokens','once');
T = vertcat(T{:});
M = str2double(T(:,1:2))
B = strcmpi(T(:,3),'true')

カテゴリ

Help Center および File ExchangeStructures についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by