Replacing string variables that satisfy some criteria by other string variables
Show older comments
Dear all,
This is my matrix
Matrix={
'country' 'values1' 'text'
'wr' [ 152] [ NaN]
'wr' [ 45152] [ NaN]
'wr' [ 654152] [ NaN]
'wr' [ 15552] 'dedfr1'
'wr' [ 155682] 'dggyyd'
'wr' [ 15445352] 'ghn23r'
'wr' [ 1545672] 'ghtyu1'
'AW' [ 142452] [ NaN]
'AW' [ 154522] [ NaN]
'AW' [ 1545242] [ NaN]
'AW' [ 154562] 'derf'
'AW' [ 15482] 'wedgrdt'
'AW' [ 1592] 'ftervd'
'JI' [ 15972] 'lofwfr'
'JI' [ 1952] [ NaN']
'JI' [ 1529] [ NaN']
'JI' [ 1592] [ NaN']
'JI' [ 151442] 'ftth'
'JI' [ 55152] 'eswq'
'JI' [ 8152] 'qwsd 23'
}
I want to replace all the text informatin that corresponds to 'Aw', that is,
AW={'derf'
'wedgrdt'
'ftervd'
'lofwfr'}
by other text information
AWnew={'AW3', 'AW2', 'AW5','AW8'};
so as to have a new matrix
Matrix={
'country' 'values1' 'text'
'wr' [ 152] [ NaN]
'wr' [ 45152] [ NaN]
'wr' [ 654152] [ NaN]
'wr' [ 15552] 'dedfr1'
'wr' [ 155682] 'dggyyd'
'wr' [ 15445352] 'ghn23r'
'wr' [ 1545672] 'ghtyu1'
'AW' [ 142452] [ NaN]
'AW' [ 154522] [ NaN]
'AW' [ 1545242] [ NaN]
'AW' [ 154562] 'AW3'
'AW' [ 15482] 'AW2'
'AW' [ 1592] 'AW5'
'JI' [ 15972] 'AW8'
'JI' [ 1952] [ NaN']
'JI' [ 1529] [ NaN']
'JI' [ 1592] [ NaN']
'JI' [ 151442] 'ftth'
'JI' [ 55152] 'eswq'
'JI' [ 8152] 'qwsd 23'
}
Similarly, I wan to replace the text information that corresponds to 'Wr' by other text information and so on..
I tried the following code only for 'AW'
list={'wr','AW' 'JI'}
for rr=2
countryName=list(rr); % which country we are referring t
Countryindic = cellfun(@(country)strcmp(country, countryName), Matrix(:,1)) ;
kkk2= find( Countryindic==1);
for k=1:numel(AW)
Matrix(kkk2,3)=cellfun(@(x) regexprep(x,AW{k},AWnew{k}),Matrix(kkk2,3),'un',0);
end
end
but I get this message
Undefined function or method 'regexprep' for input arguments of type 'double'.
Error in ==> @(x) regexprep(x,AW{k},AWnew{k})
Is there any other code that could get this job done efficienty or quickly?
My real matrix is 40000 by 29
thanks
Accepted Answer
More Answers (1)
Jos (10584)
on 4 Feb 2013
looping over strrep would do, I presume:
A = {...}
Anew = {...}
for k = 1: ...,
Matrix(:,3) = strrep(Matrix(:,3), A{k},Anew{k})
end
Categories
Find more on Characters and Strings 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!