Filter löschen
Filter löschen

Place filenames with common pattern in a cell array

2 Ansichten (letzte 30 Tage)
Davindra Usov
Davindra Usov am 24 Apr. 2023
Kommentiert: Davindra Usov am 25 Apr. 2023
Hi there,
If I have a cell array containing filenames in the following format 'breakpoint_b40_f15.txt', 'breakpoint_b50_f15.txt', 'breakpoint_b40_f20.txt', 'breakpoint_b50_f20.txt', how do I create a new cell array where each row contains the files with the same number after 'f'? for example. 'breakpoint_b40_f15.txt' and 'breakpoint_b50_f15.txt' should be in the same row, and 'breakpoint_b40_f20.txt' and 'breakpoint_b50_f20.txt' should be in the same row but in a different row to the f15 row.
Many thanks
  2 Kommentare
Stephen23
Stephen23 am 24 Apr. 2023
Verschoben: Stephen23 am 24 Apr. 2023
C = {'breakpoint_b40_f15.txt', 'breakpoint_b50_f15.txt', 'breakpoint_b40_f20.txt', 'breakpoint_b50_f20.txt'};
D = reshape(C,2,2).'
D = 2×2 cell array
{'breakpoint_b40_f15.txt'} {'breakpoint_b50_f15.txt'} {'breakpoint_b40_f20.txt'} {'breakpoint_b50_f20.txt'}
Davindra Usov
Davindra Usov am 24 Apr. 2023
Verschoben: Stephen23 am 24 Apr. 2023
Thanks a bunch Stephen. This works but how can I change the number of rows of D to be the number of unique f values, which in this case is 2 (15 and 20)?

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

dpb
dpb am 24 Apr. 2023
Verschoben: Image Analyst am 24 Apr. 2023
C = {'breakpoint_b40_f15.txt', 'breakpoint_b50_f15.txt', 'breakpoint_b40_f20.txt', 'breakpoint_b50_f20.txt', 'breakpoint_b40_f25.txt','breakpoint_b50_f25.txt'};
N=numel(unique(extractBetween(extractAfter(C,'breakpoint_'),'_','.txt')));
reshape(C,[],N).'
Relies upon the sequence being already sorted in pairs; otherwise need to locate the elements position if not already sorted -- or sort first by the second substring of interest. Another case where putting metadata into the filenames makes things harder than if were real data in a database file....seems to be a spate of those recently.

Weitere Antworten (2)

dpb
dpb am 24 Apr. 2023
Verschoben: Image Analyst am 24 Apr. 2023
C = {'breakpoint_b40_f15.txt', 'breakpoint_b50_f15.txt', 'breakpoint_b40_f20.txt', 'breakpoint_b50_f20.txt'}.';
N=(extractBetween(extractAfter(C,'breakpoint_'),'_','.txt'))
N = 4×1 cell array
{'f15'} {'f15'} {'f20'} {'f20'}
uN=unique(N)
uN = 2×1 cell array
{'f15'} {'f20'}
Add/use N as a grouping variable or sort by indexing array...

Kevin Holly
Kevin Holly am 24 Apr. 2023
Verschoben: Image Analyst am 24 Apr. 2023
C = {'breakpoint_b40_f15.txt', 'breakpoint_b50_f15.txt', 'breakpoint_b40_f20.txt', 'breakpoint_b50_f20.txt', 'breakpoint_b40_f25.txt','breakpoint_b50_f25.txt'};
D = strfind(C,'breakpoint_b40_f');
f = length([D{:}])
f = 3
D = strfind(C,'_f15');
b = length([D{:}])
b = 2
D = reshape(C,b,f).'
D = 3×2 cell array
{'breakpoint_b40_f15.txt'} {'breakpoint_b50_f15.txt'} {'breakpoint_b40_f20.txt'} {'breakpoint_b50_f20.txt'} {'breakpoint_b40_f25.txt'} {'breakpoint_b50_f25.txt'}

Kategorien

Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange

Produkte


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by