Sort cell strings according to specific subsets of those cell strings
Show older comments
Let's say I have a cell string with values:
filename = {'2009.272.17.57.23.8445.AZ.SMER..BHE.R.SAC';...
'2009.272.17.57.24.5500.AZ.FRD..BHN.R.SAC';...
'2009.272.17.57.27.5445.AZ.SMER..BHN.R.SAC';...
'2009.272.17.57.27.8000.AZ.SND..BHZ.R.SAC';...
'2009.272.17.57.27.9445.AZ.BZN..BHE.R.SAC';...
'2009.272.17.57.28.7000.AZ.SND..BHN.R.SAC';...
'2009.272.17.57.29.1250.AZ.FRD..BHZ.R.SAC';...
'2009.272.17.57.29.2250.AZ.PFO..BHE.R.SAC';...
'2009.272.17.57.29.3695.AZ.SMER..BHZ.R.SAC';...
'2009.272.17.57.29.9445.AZ.BZN..BHN.R.SAC';...
'2009.272.17.57.30.0000.AZ.RDM..BHN.R.SAC';...
'2009.272.17.57.30.8000.AZ.RDM..BHZ.R.SAC';...
'2009.272.17.57.31.8250.AZ.LVA2..BHZ.R.SAC';...
'2009.272.17.57.31.8500.AZ.LVA2..BHE.R.SAC';...
'2009.272.17.57.31.9195.AZ.BZN..BHZ.R.SAC';...
'2009.272.17.57.32.0000.AZ.WMC..BHZ.R.SAC';...
'2009.272.17.57.32.6750.AZ.WMC..BHN.R.SAC';...
'2009.272.17.57.33.3195.AZ.KNW..BHZ.R.SAC';...
'2009.272.17.57.33.4750.AZ.TRO..BHN.R.SAC';...
'2009.272.17.57.33.7750.AZ.PFO..BHN.R.SAC';...
'2009.272.17.57.33.9000.AZ.PFO..BHZ.R.SAC';...
'2009.272.17.57.34.1750.AZ.LVA2..BHN.R.SAC';...
'2009.272.17.57.34.8000.AZ.TRO..BHZ.R.SAC';...
'2009.272.17.57.35.0000.AZ.WMC..BHE.R.SAC';...
'2009.272.17.57.35.0750.AZ.RDM..BHE.R.SAC';...
'2009.272.17.57.35.8945.AZ.KNW..BHE.R.SAC';...
'2009.272.17.57.36.0250.AZ.FRD..BHE.R.SAC';...
'2009.272.17.57.36.2250.AZ.CRY..BHZ.R.SAC';...
'2009.272.17.57.36.3500.AZ.CRY..BHN.R.SAC';...
'2009.272.17.57.36.4500.AZ.SND..BHE.R.SAC';...
'2009.272.17.57.36.5000.AZ.TRO..BHE.R.SAC';...
'2009.272.17.57.36.5195.AZ.KNW..BHN.R.SAC';...
'2009.272.17.57.36.5750.AZ.CRY..BHE.R.SAC'};
I want to be able to assume that I do not know what character the station name (e.g., CRY) or component name (e.g., BHE) starts and ends on. Though, the number of periods (".") will be consistent.
I have something fairly clunky to do this, but I am wondering if anyone can suggest a quick one/two-liner that would assume a string format of the general form:
YYYY.DDD.HH.MM.SS.ssss.$1.$2..$3.R.SAC
where:
$1 = Array name $2 = Station name $3 = Component name
And then sort the list with the primary and secondary sort order according to $2 and $3, respectively, so that the first 6 rows in the cell string would be:
2009.272.17.57.27.9445.AZ.BZN..BHE.R.SAC
2009.272.17.57.29.9445.AZ.BZN..BHN.R.SAC
2009.272.17.57.31.9195.AZ.BZN..BHZ.R.SAC
2009.272.17.57.36.5750.AZ.CRY..BHE.R.SAC
2009.272.17.57.36.3500.AZ.CRY..BHN.R.SAC
2009.272.17.57.36.2250.AZ.CRY..BHZ.R.SAC
...
4 Comments
Walter Roberson
on 22 Jan 2012
In the example you show, the number of characters for each component is exactly the same for each line. Is that a rule for your situation? If so then the sort becomes quite simple.
Dr. Seis
on 22 Jan 2012
Jan
on 22 Jan 2012
It looks like the parts do *not* have the same length:
'2009.272.17.57.33.9000.AZ.PFO..BHZ.R.SAC'
'2009.272.17.57.34.1750.AZ.LVA2..BHN.R.SAC'
Dr. Seis
on 22 Jan 2012
Accepted Answer
More Answers (2)
Jan
on 22 Jan 2012
filename = {'2009.272.17.57.23.8445.AZ.SMER..BHE.R.SAC';...
'2009.272.17.57.24.5500.AZ.FRD..BHN.R.SAC';...
'2009.272.17.57.27.5445.AZ.SMER..BHN.R.SAC';...
'2009.272.17.57.27.8000.AZ.SND..BHZ.R.SAC';...
'2009.272.17.57.27.9445.AZ.BZN..BHE.R.SAC';...
'2009.272.17.57.28.7000.AZ.SND..BHN.R.SAC';...
'2009.272.17.57.29.1250.AZ.FRD..BHZ.R.SAC';...
'2009.272.17.57.29.2250.AZ.PFO..BHE.R.SAC'};
n = numel(filename);
C2 = cell(1, n);
C3 = cell(1, n);
for iC = 1:n
D = textscan(filename{iC}(27:end), '%s', 'Delimiter', '.');
C2{iC} = D{1}{1};
C3{iC} = D{1}{3};
end
% A kind of SORTROWS:
[dummy, ind3] = sort(C3);
[dummy, ind2] = sort(C2(ind3));
index = ind3(ind2);
filename = filename(index);
Dr. Seis
on 22 Jan 2012
Categories
Find more on Shifting and Sorting Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!