Finding substring based on another substring

1 回表示 (過去 30 日間)
Derek Handwerk
Derek Handwerk 2019 年 3 月 28 日
編集済み: Image Analyst 2019 年 3 月 29 日
I have a bunch of data files and can get all of their names via
D = dir('*.mat');
Each name has a couple of numbers in it in the format '*num1_###*num2_###*.mat' where ### is the actual number.
I can set num1 equal to some value, say 1.7, and then get all assotiated files i.e.
D = dir('*num1_1.7*)
essentially getting a row of num2 files for num1 = 1.7 if you think of num1 and num2 making a grid of values.
What is the most efficient way to get files along the diagonal? I.e. where num1 is equal to num2, but the values aren't known a priori.
For example, if I have the files
num1_1_num2_1.mat num1_2_num2_1.mat num1_3_num2_1.mat
num1_1_num2_2.mat num1_2_num2_2.mat num1_3_num2_2.mat
num1_1_num2_2.mat num1_2_num2_3.mat num1_3_num2_3.mat
from dir(), how can I extract
num1_1_num2_1.mat, num1_2_num2_2.mat, num1_3_num2_3.mat?

採用された回答

Stephen23
Stephen23 2019 年 3 月 28 日
編集済み: Stephen23 2019 年 3 月 28 日
"What is the most efficient way to get files along the diagonal?"
Probably something like this is about as efficient as you can get with MATLAB:
S = dir('*.mat');
N = {S.name};
M = sscanf([N{:}],'num1_%d_num2_%d.mat',[2,Inf])
Z = N(M(1,:)==M(2,:))

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeWhos についてさらに検索

タグ

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by