How to grab Numbers within a string

Hi, I'll try to be as concise as possible.
So I have a document full of files, where the filename denotes the coordinates of the dataset.
As an example, one file is called "Dx(005_50);Dy(095_25)" so it is 5.5mm on the x-axis and 95.25mm on the y axis. I have Matlab reading in the files and plotting the data in the correct coordinates when the naming convention used to just be "005_50_095_25" as I just used regexp and 'split' to seperate the numbers into different matrix positions.
I basically want to have Matlab do the same thing with the new naming conventions although I haven't had loads of experience with regexp and have tried using 'tokens' to no avail.
I basically want a result of;
ans = [005 , 50, 095, 25]
if that makes sense.
Cheers.

 採用された回答

Adam Danz
Adam Danz 2018 年 12 月 11 日

1 投票

There are two different outputs. The first preserves the leading 0s by keeping the numbers in string format. The second puts the numbers into numerical format.
s = 'Dx(005_50);Dy(095_25)';
numStr = regexp(s, '\d+', 'match');
numStr =
1×4 cell array
{'005'} {'50'} {'095'} {'25'}
numVec = str2double(numStr);
numVec =
5 50 95 25

2 件のコメント

Ryan Shone
Ryan Shone 2018 年 12 月 11 日
This has given me what I wanted and taken it a step further, really appreciate the quick response, didn't think to use 'match' rather than 'split' cannot thank you enough!
Adam Danz
Adam Danz 2018 年 12 月 11 日
Glad it worked out!

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

質問済み:

2018 年 12 月 11 日

コメント済み:

2018 年 12 月 11 日

Community Treasure Hunt

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

Start Hunting!

Translated by