I want to change the name of elements in a variable
4 views (last 30 days)
Show older comments
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
on 10 Oct 2021
Was there a user interface when you imported the data? If so, there was an option on how to import it. I believe the default is a table. Can you look in the workspace window and tell me what variables you see. It should state the type of variable as well.
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")
Accepted Answer
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"
2 Comments
More Answers (0)
See Also
Categories
Find more on Characters and Strings 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!