I want to change the name of elements in a variable

2 views (last 30 days)
Krutik Gamit
Krutik Gamit on 10 Oct 2021
Commented: Kevin Holly on 10 Oct 2021
I have a variable name countries, and there are few countries in there; UK, Canada, France, Italy, Japan and United States. How do I change the name 'United States' to 'US' in script?
  6 Comments
Kevin Holly
Kevin Holly on 10 Oct 2021
If the country names repeat in an array and you wanted to replace all of the "United States" with "US", you can use strrep .
strrep(Countries,"United States","US")

Sign in to comment.

Accepted Answer

Kevin Holly
Kevin Holly on 10 Oct 2021
Edited: Kevin Holly on 10 Oct 2021
You can save a string array with those names as such:
Countries = ["UK";"Canada"; "France";"Italy";"Japan";"United States"];
You can change the 6th element in the array as such:
Countries(6) = "US"

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!