I have the following text file which contains the following text lines: C:\Users\David\ab_1.obj
C:\Users\David\ab_5.obj
C:\Users\David\ab_300.obj
C:\Users\David\ab_27.obj
C:\Users\David\ab_233.obj
C:\Users\David\ab_1232.obj
C:\Users\David\ab_22.obj
I want to extract the numbers attached to the file names and the resulted numbers should stored in array
A=[1,5,22,27,233,300,1232]

 采纳的回答

There are many ways of doing this, depending on what the criteria is for extracting the numbers (is it any number in the file? only those in the pattern ab_*.obj, something else?)
Anyway:
filecontent = fileread('yourfile.txt');
numbers = str2double(regexp(filecontent, '(?<=ab_)\d+(?=\.obj)', 'match'))

更多回答(0 个)

类别

在 帮助中心 和 File Exchange 中查找有关 Characters and Strings 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by