Find and replace value in a table
Show older comments
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!
Answers (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)
Abbrev = {'AUS'; 'GER'; 'CAN'};
for k = 1:numel(Abbrev)
Samples.Location(strcmp(Samples.Location,Country{k})) = Abbrev(k);
end
Samples
The ‘Abbrev’ vector can of course be expanded to include abbreviations for the other ‘Country’ elements as well, if desired.
.
2 Comments
Celina Brinkmann
on 9 Oct 2021
Star Strider
on 10 Oct 2021
My pleasure!
If my Answer helped you solve your problem, please Accept it!
.
Categories
Find more on Library Development 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!