Finding a letter or number in a string of cells

3 Ansichten (letzte 30 Tage)
Matthew
Matthew am 29 Okt. 2015
Bearbeitet: Stephen23 am 29 Okt. 2015
Example. Say I have a cell array = Brad1, Bobby2, 1Bob, 2Bradley, 2Bailey. etc... Each Name is a file of information containing some attributes. Say this cell array of names is about 180 files long.
I need to separate the names, i.e put them in either a cell array for names that contain a 1, or place them into an array if their name contains a 2. How would I do this using a for loop?.

Antworten (3)

TastyPastry
TastyPastry am 29 Okt. 2015
out = {};
for i=1:numel(myData)
myStr = myData{i};
myNum = str2double(myStr(myStr>= 48 & myStr <= 57));
myName = myStr(isstrprop(myStr,'alpha'));
out{size(out,1)+1,myNum} = myName;
end
This stores the data into a cell array where the columns correspond to the values in the names.

Thorsten
Thorsten am 29 Okt. 2015
Bearbeitet: Thorsten am 29 Okt. 2015
C = {'Brad1', 'Bobby2', '1Bob', '2Bradley', '2Bailey'}
f1 = C(~cellfun(@isempty, strfind(C, '1')))
f2 = C(~cellfun(@isempty, strfind(C, '2')))

Stephen23
Stephen23 am 29 Okt. 2015
Bearbeitet: Stephen23 am 29 Okt. 2015
There is no need for any loops:
>> X = {'Brad1','Bobby2','1Bob','2Bradley','2Bailey'};
>> X(~cellfun('isempty',strfind(X,'1')))
ans = 'Brad1' '1Bob'
>> X(~cellfun('isempty',strfind(X,'2'));)
ans = 'Bobby2' '2Bradley' '2Bailey'

Kategorien

Mehr zu Loops and Conditional Statements 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