How can I use the unique function to output all the different numbers following M from all the file names?

1 Ansicht (letzte 30 Tage)
rootdir = '/Users/apple/Desktop/Project/Code/Data/';
l = dir([rootdir,'*_dat.mat']);
for n=1:length(l)
str=l(n).name;
ix = strfind(str,'_');
%id = 'M70_20141020';
%blk = 1;
id=str(1:ix(2)-1);
blk=str2num(str(ix(2)+2:ix(3)-1));
date=str(ix(1)+1:ix(1)+8);
fprintf('%d:%s,blk%d\n',n,id,blk);
mousenum=str(ix(1)-2:ix(1)-1);
nmouse=str2num(mousenum);
end
f=unique(nmouse)
the output is
1:M25_20130506,blk2
2:M25_20130507,blk1
3:M25_20130508,blk1
4:M25_20130509,blk1
5:M25_20130510,blk1
6:M25_20130511,blk2
7:M25_20130513,blk1
8:M25_20130513b,blk3
9:M25_20130514,blk1
10:M25_20130514b,blk3
11:M25_20130515b,blk3
12:M26_20130506,blk2
13:M26_20130507,blk1
14:M26_20130508,blk2
15:M26_20130509,blk1
16:M26_20130510,blk1
17:M26_20130511,blk2
18:M26_20130513,blk1
19:M26_20130513b,blk3
20:M26_20130514,blk1
21:M26_20130514b,blk3
22:M26_20130515,blk1
23:M26_20130515b,blk2
24:M27_20130506,blk1
25:M27_20130507,blk1
26:M27_20130508,blk1
27:M27_20130509,blk1
28:M27_20130510,blk1
29:M27_20130511,blk2
30:M27_20130513,blk1
31:M27_20130513b,blk3
32:M27_20130514b,blk3
33:M27_20130515b,blk2
34:M28_20130506,blk2
35:M28_20130507,blk1
36:M28_20130508,blk1
37:M28_20130509,blk1
38:M28_20130510,blk1
39:M28_20130511,blk2
40:M28_20130513,blk1
41:M28_20130514,blk1
42:M28_20130515,blk1
43:M28_20130515b,blk3
44:M31_20130603,blk2
45:M31_20130603b,blk3
46:M31_20130604,blk3
47:M31_20130605,blk4
48:M31_20130606,blk2
49:M31_20130607,blk3
50:M31_20130608,blk2
51:M31_20130609,blk1
52:M31_20130609b,blk2
53:M31_20130610,blk1
54:M31_20130610b,blk3
55:M31_20130611,blk2
56:M31_20130611b,blk8
57:M31_20130612,blk1
58:M31_20130612b,blk2
59:M70_20141022,blk1
60:M70_20141028,blk1
61:M71_20141020,blk1
62:M71_20141029,blk1
63:M72_20141020,blk1
64:M72_20141101,blk1
65:M73_20141020,blk1
66:M73_20141028,blk1
67:M75_20141021,blk1
68:M75_20141029,blk1
69:M80_20141023,blk1
70:M80_20141028,blk1
71:M81_20141021,blk1
72:M81_20141029,blk1
73:M87_20141021,blk1
74:M87_20141028,blk1
75:M89_20141021,blk1
76:M89_20141029,blk1
77:M93_20141023,blk1
78:M93_20141028,blk1
f =
93
Only the number after M of the name of the last file the loop goes over is given.
How can I use the unique function to output all the different numbers following M from all the file names?

Akzeptierte Antwort

Stephen23
Stephen23 am 8 Nov. 2020
Bearbeitet: Stephen23 am 8 Nov. 2020
I doubt that you need a loop. You could use a simple regular expression to get the required digits, e.g.:
D = '/Users/apple/Desktop/Project/Code/Data/';
S = dir(fullfile(D,'*_dat.mat')); % much better to use FULLFILE than concatenation
C = regexp({S.name},'(?<=M)\d+','match','once');
V = unique(str2double(C))

Weitere Antworten (1)

dpb
dpb am 8 Nov. 2020
rootdir = '/Users/apple/Desktop/Project/Code/Data/'; % good! Using variable so can change: +1
d=dir(fullfile(rootdir,'*_dat.mat')); % use fullfile() instead string operations
str=split(string({d.name}).',{'_',','}); % break into pieces after make string array
nmouse=str2double(extractAfter(str(:,1),'M')); % convert number
dates=str(:,2); % would done datetime() but for trailing 'b'
blk=str(:,3); % save the last piece
unmouse=unique(nmouse); % get unique numbers
Or, just continue to use the array instead of making new variables that are copies. Can clear the str array now if use the other variables.
As noted, you'll have to decide how to treat the trailing character on the date strings to turn into datetime variables--BTW, there's a builtin date function so didn't use it for variable name to not alias. Finding those longer than the 8 character data and extracting that last character would be start...there's only one 'b' value in this dataset; whether there could be 'c' or higher dunno so didin't make assumptions.

Kategorien

Mehr zu Data Type Conversion finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by