Find and replace value in a table
64 次查看(过去 30 天)
显示 更早的评论
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!
0 个评论
回答(1 个)
Star Strider
2021-10-9
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 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!