Replacing string variables that satisfy some criteria by other string variables

1 vue (au cours des 30 derniers jours)
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

Réponse acceptée

Azzi Abdelmalek
Azzi Abdelmalek le 4 Fév 2013
Modifié(e) : Azzi Abdelmalek le 4 Fév 2013
AW={'derf','wedgrdt','ftervd','lofwfr'}
AWnew={'AW3', 'AW2', 'AW5','AW8'};
for k=1:numel(AW)
Matrix(find(strcmp(Matrix,AW{k})))={AWnew{k}}
end
  4 commentaires
tzaloupas
tzaloupas le 4 Fév 2013
Perfect! thanks a lot!!!!!
Azzi Abdelmalek
Azzi Abdelmalek le 4 Fév 2013
Modifié(e) : Azzi Abdelmalek le 4 Fév 2013
%or simpler
col3=Matrix(:,3);
AW={'derf', 'wedgrdt','ftervd','lofwfr'}
AWnew={'AW3', 'AW2', 'AW5','AW8'};
for k=1:numel(AW)
col3(find(strcmp(col3,AW{k})))={AWnew{k}}
end
M=[Matrix col3]

Connectez-vous pour commenter.

Plus de réponses (1)

Jos (10584)
Jos (10584) le 4 Fév 2013
looping over strrep would do, I presume:
A = {...}
Anew = {...}
for k = 1: ...,
Matrix(:,3) = strrep(Matrix(:,3), A{k},Anew{k})
end

Catégories

En savoir plus sur Characters and Strings dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by