Hi,
I am working with the table 'Samples' and I would like to find and replace values of the column 'Location'.
I would like to find the value 'Germany' and replace it with 'GER', find 'Australia' and replace it with 'AUS' and find 'Canada' and replace it with 'CAN'. How can I find these values and replace them?
I am new with matlab and would really appreciate any help!

Réponses (1)

Try something like this —
Country = {'Australia';'Germany';'Canada';'United Kingdom';'France'};
Location = Country(randi(numel(Country),10,1));
Something = randn(10,1);
Samples = table(Location, Something)
Samples = 10×2 table
Location Something __________________ _________ {'Germany' } -0.64263 {'Australia' } 0.050271 {'Australia' } -0.02538 {'Canada' } -1.4615 {'United Kingdom'} -0.84455 {'Germany' } -0.54954 {'Australia' } 0.23321 {'France' } -0.052465 {'Australia' } 1.3437 {'Canada' } 0.33439
Abbrev = {'AUS'; 'GER'; 'CAN'};
for k = 1:numel(Abbrev)
Samples.Location(strcmp(Samples.Location,Country{k})) = Abbrev(k);
end
Samples
Samples = 10×2 table
Location Something __________________ _________ {'GER' } -0.64263 {'AUS' } 0.050271 {'AUS' } -0.02538 {'CAN' } -1.4615 {'United Kingdom'} -0.84455 {'GER' } -0.54954 {'AUS' } 0.23321 {'France' } -0.052465 {'AUS' } 1.3437 {'CAN' } 0.33439
The ‘Abbrev’ vector can of course be expanded to include abbreviations for the other ‘Country’ elements as well, if desired.
.

2 commentaires

Celina Brinkmann
Celina Brinkmann le 9 Oct 2021
Thank you!
Star Strider
Star Strider le 10 Oct 2021
My pleasure!
If my Answer helped you solve your problem, please Accept it!
.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Language Fundamentals dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by