Extract number and infromation from multiple image files ?

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

 Accepted Answer

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 Comments

Thank you Mr. Rik, it works smoothly
And i want to ask some more, is there any way to calculate the number in function for example
for i= 1:lenghth(N)
(c{1}/45)*c{2}
end
Depending on what you want to do with the number it is probably easiest to modify the function:
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
ohhh, thankss, stay safe in this time position
If i want to show a table with c{4} and number of value but it seem not working well
k=count(c);
bar(k,c{4});
xlabel('Number of images');
ylabel('Range of values');
You need to distinguish between inside and outside the MyFun function. Look at what variable you have. What data type is it? What size? What code did you use exactly?
i believe it is still in the function
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}/c{2}^2)*999;
k=count(c);
bar(k,c{4});
xlabel('Number of images');
ylabel('Range of values');
but it does not work well!!
You really shouldn't do that. You should split the task of parsing your data from the task of displaying that data. The Myfun function will run in a loop, so it will create many bar objects if you do it this way.
Thank you, so is there anyway to call out the c4 from the funtion ?
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}/c{2}^2)*999;
k=length(c);
for i=k
c5=c{1,i}{1,4};
end
plot(c5,k,'*')
xlabel('Number of images');
ylabel('Range of values');
end
Don't do any further processing in that function. The plotting should not be inside that function. You should only use it to extract values from the file names, nothing more. Anything you want to do with those values should happen outside of that function.
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

Sign in to comment.

More Answers (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')

Categories

Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange

Tags

Asked:

on 25 Jun 2020

Commented:

on 7 Jul 2020

Community Treasure Hunt

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

Start Hunting!