Find and Replace (MATLAB)
33 views (last 30 days)
Show older comments
Deborah Johnson
on 7 Jun 2022
Edited: Deborah Johnson
on 11 Jun 2022
I am trying to find and replace text string before a comma and keep the data string after the comma. I have tried the wildcard '*' but it didn't work. I have 55 states to do this to. An example is 'Adak Island, AK' to just "AK".
repeats = ' , AK'
indices = strfind(repeats, 'AK')
indices = 4
using_replace = replace(repeats, ' , AK', 'AK')
nothing changed, the text string stayed the same. Any suggestions?
0 Comments
Accepted Answer
Sean de Wolski
on 7 Jun 2022
strip(extractAfter('Adak Island, AK',","))
And see the other fun methods of string to figure out how to manipulate the part you want
methods('string')
3 Comments
Sean de Wolski
on 7 Jun 2022
This will stick it back in as a new variable in the table called State
T = readtable('County_STATE.csv');
T.State = extractAfter(T.DISPLAY_AIRPORT_CITY_NAME_FULL,", ");
More Answers (4)
Image Analyst
on 10 Jun 2022
Edited: Image Analyst
on 10 Jun 2022
Try this:
t = readtable('County_STATE.csv');
% Crop off null columns
t = t(:, 1:3);
% Extract State alone, and City alone.
State = extractAfter(t.DISPLAY_AIRPORT_CITY_NAME_FULL, ', ');
City = extractBefore(t.DISPLAY_AIRPORT_CITY_NAME_FULL, ', ');
% Add these on as columns in the table variable.
t = addvars(t, City, State, 'After','DISPLAY_AIRPORT_CITY_NAME_FULL')
0 Comments
Image Analyst
on 7 Jun 2022
Why not just do this:
str = 'Adak Island, AK' ;
commaIndex = strfind(str, ',')
if ~isempty(commaIndex)
output = strtrim(str(commaIndex+1 : end))
else
output = str % No comma in the string so just return the original.
end
4 Comments
Image Analyst
on 7 Jun 2022
You gave us a character array. Please give us the data in the form of a table in a mat file with the paperclip icon so we can show you how to apply it to a table. Did you try just going down the column row by row?
Steven Lord
on 7 Jun 2022
Functions like extract, extractAfter, etc. don't modify the variables that you pass in as input. If you were to specify those same variable names as the output argument for those calls then they would be modified.
fruit = "Apple, Banana, Cherry"
The following call creates a new variable named justCherry and leaves the fruit variable with its original contents.
justCherry = extractAfter(fruit, "Banana, ")
fruit
The following call overwrites the existing variable named fruit with the output of extractAfter, so fruit is different after the call.
fruit = extractAfter(fruit, "Banana, ")
fruit
Your original code using_replace = replace(repeats, ' , AK', 'AK') does not modify the repeats variable.
If you'd written repeats = replace(repeats, ' , AK', 'AK') that would overwrite the original contents of the repeats variable with the modified data returned by replace.
0 Comments
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!