MATLAB Answers

How can you read a filename but store a specific number within it?

1 view (last 30 days)
Hi All,
I'm trying to analyse a bunch of files that have filenames like "1 crop 1, area - 122637", "1 crop 2, area - 88497" etc.
I need to store the numbers proceeding "area - " as a number and sum them for all the files within a folder.
Is this possible?
Thanks
  2 Comments
Teshan Rezel
Teshan Rezel on 5 May 2020
hi Stephen, thanks for responding. "Proceeding" can also mean "originating from" and can be considered the opposite to "preceed". Apologies for the confusion!
To clarify, I used the word in the same context you have used the word "following"...so the latter case in your examples.
They have a .jpg file extension.
Many Thanks!

Sign in to comment.

Accepted Answer

Stephen Cobeldick
Stephen Cobeldick on 5 May 2020
Edited: Stephen Cobeldick on 5 May 2020
D = 'path to the folder where the files are saved';
S = dir(fullfile(D,'*crop*area*.jpg'));
[~,N] = cellfun(@fileparts,{S.name},'uni',0);
V = str2double(regexp(N,'\d+$','match','once'));
Z = sum(V)

More Answers (0)

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!