I want to change the name of elements in a variable

4 views (last 30 days)
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
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
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)

Categories

Find more on Characters and Strings in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!