If I have file names listed in a readtable how do I get it to extract part of the file name?

18 views (last 30 days)
So what I'm trying to do is extract part of a file name from a column in a readtable. I already have the specific column assigned to a variable but I'm trying to create a path which requires me to pull part of the filename. Here's an example:
This is one of several hundred file names.
SSSM 5 - 15Nov17_1152 - GE - right - 9.fantasy.tiff
I need:
15Nov17_1152
It is in the same place for all the files. I have looked at several previous questions but I am really new to coding and the all have underscores around there's. If someone could solve this example for me and explain (in simple terms)how to apply it to several hundred file names.

Accepted Answer

Paolo
Paolo on 14 Jun 2018
Edited: Paolo on 14 Jun 2018
You have two options for extracting that data. The first is the simplest one. Since you said that that information always occurs at the same location, you can simply use fixed indices. Alternatively, you can use regexp to match your entry anywhere in the file.
For string x = 'SSSM 5 - 15Nov17_1152 - GE - right - 9.fantasy.tiff':
Option 1.
x(10:21) = '15Nov17_1152'
Option 2.
var = regexp(x,'\b(\d*\w*\d*_\d*)\b','match')
var{1} =
'15Nov17_1152'
The regular expression pattern matches digits, followed by a word characters, followed by digits, an underscore and finally more digits.
  6 Comments
Paolo
Paolo on 14 Jun 2018
Edited: Paolo on 14 Jun 2018
Perhaps you should have specified that in your question. I do not know the names of the files that you are trying to load. Do you want to open images in a loop or ... ?
I hope this example can help you. Let's say in those folders you have .jpg files. The following code has an outer loop which executes for number of times equal to the number of 'paths' that you have in your variable. For every folder, the program checks to see how many .jpg files it can find. This is done in the inner for loop with the use of dir function. For every file .jpg file it finds, it opens it with imread . All images are saved to a cell array named images. You can use the images in the cell array images for processing as you wish.
images = {};
for i=1:numel(data)
files = dir(strcat(var{i},'/*.jpg'));
for j=1:numel(files)
images = [images;imread(fullfile(files(j).folder,files(j).name))];
end
end
images =
3×1 cell array
{900×1920×3 uint8}
{900×1920×3 uint8}
{900×1920×3 uint8}
Put for now all i need is the part of the file name assigned to a variable.
In the previous code snippet, all the variables can be accessed from var with:
var{1} = '15Nov17_1152'
var{2} = '15Nov18_1153'
var{3} = '15Nov18_1156'
... etc

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!