How I can select the maximum number in my filename?

2 ビュー (過去 30 日間)
Rita
Rita 2015 年 8 月 19 日
コメント済み: Rita 2015 年 8 月 19 日
I really would appreciate if anyone could solve my problem. I have
net1_0.2327.mat
net1_0.3425.mat
net2_0.8765.mat
net2_0.6754.mat ,......to net50_0.87654.mat net50_0.3456.mat
I would like to get these net1_0.3425.mat net2_0.8765.mat ,.... the max of each net1 to net net50(comparing net1 with net1 and net2 with net2 and so on) I used the script
dinfo = dir('net*.mat');
filenames = {dinfo.name};
parts = regexp(filenames, '_', 'split');
part2 = cellfun(@(C) C{2}, parts, 'Uniform',0);
parts3 = regexp(part2, '.mat', 'split');% to remove mat extension
val2 = str2double(parts3)% I get NaN instead of numbers!
Thanks in advance for your help.

採用された回答

Walter Roberson
Walter Roberson 2015 年 8 月 19 日
dinfo = dir('net*.mat');
filenames = {dinfo.name};
filenumstr = regexprep(filenames, {'^[^_]+_', '\.mat$'}, {'', ''});
val2 = str2double(filenumstr);

その他の回答 (1 件)

Morteza
Morteza 2015 年 8 月 19 日
Change your code like below:
clc,clear all
dinfo = dir('net*.mat');
filenames = {dinfo.name};
parts = regexp(filenames, '_', 'split');
part2 = cellfun(@(C) C{2}, parts, 'Uniform',0);
parts3 = regexp(part2, '.mat', 'split');% to remove mat extension
for i = 1:length(parts3)
val2(i) = str2double(cell2mat(parts3{1,i}));
end
  1 件のコメント
Rita
Rita 2015 年 8 月 19 日
Thank you so much Morteza.

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

カテゴリ

Help Center および File ExchangeCorrelation and Convolution についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by