Place filenames with common pattern in a cell array
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
dpb
2023-4-24
移动:Image Analyst
2023-4-24
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.
更多回答(2 个)
dpb
2023-4-24
移动:Image Analyst
2023-4-24
C = {'breakpoint_b40_f15.txt', 'breakpoint_b50_f15.txt', 'breakpoint_b40_f20.txt', 'breakpoint_b50_f20.txt'}.';
N=(extractBetween(extractAfter(C,'breakpoint_'),'_','.txt'))
uN=unique(N)
Add/use N as a grouping variable or sort by indexing array...
0 个评论
Kevin Holly
2023-4-24
移动:Image Analyst
2023-4-24
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{:}])
D = strfind(C,'_f15');
b = length([D{:}])
D = reshape(C,b,f).'
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Operators and Elementary Operations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!