Find and Replace (MATLAB)

41 次查看(过去 30 天)
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?

采纳的回答

Sean de Wolski
Sean de Wolski 2022-6-7
strip(extractAfter('Adak Island, AK',","))
ans = 'AK'
And see the other fun methods of string to figure out how to manipulate the part you want
methods('string')
Methods for class string: append contains eq extractAfter gt issorted lt pad reverse startsWith cellstr count erase extractBefore insertAfter join matches plus sort strip char double eraseBetween extractBetween insertBefore le ne replace split strlength compose endsWith extract ge ismissing lower or replaceBetween splitlines upper
  3 个评论
Sean de Wolski
Sean de Wolski 2022-6-7
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,", ");
Deborah Johnson
Deborah Johnson 2022-6-10
Hello Sean,
This worked! All the rows were edited and only leaving the STATE name. I repeated the same step to verify and Thank You!
T.State = extractAfter(T.DISPLAY_AIRPORT_CITY_NAME_FULL,", ");

请先登录,再进行评论。

更多回答(4 个)

Image Analyst
Image Analyst 2022-6-10
编辑:Image Analyst 2022-6-10
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')
t = 18×5 table
AIRPORT DISPLAY_AIRPORT_NAME DISPLAY_AIRPORT_CITY_NAME_FULL City State _______ _____________________________________ ______________________________ _______________ ______ {'ADK'} {'Adak NS' } {'Adak Island, AK'} {'Adak Island'} {'AK'} {'ANC'} {'Anchorage International' } {'Anchorage, AK' } {'Anchorage' } {'AK'} {'ADQ'} {'Kodiak Airport' } {'Kodiak, AK' } {'Kodiak' } {'AK'} {'AKN'} {'King Salmon Airport' } {'King Salmon, AK'} {'King Salmon'} {'AK'} {'BET'} {'Bethel Airport' } {'Bethel, AK' } {'Bethel' } {'AK'} {'BRW'} {'Wiley Post/Will Rogers Memorial' } {'Barrow, AK' } {'Barrow' } {'AK'} {'CDV'} {'Cordova Mile 13' } {'Cordova, AK' } {'Cordova' } {'AK'} {'DLG'} {'Dillingham Airport' } {'Dillingham, AK' } {'Dillingham' } {'AK'} {'FAI'} {'Fairbanks International' } {'Fairbanks, AK' } {'Fairbanks' } {'AK'} {'GST'} {'Gustavus Airport' } {'Gustavus, AK' } {'Gustavus' } {'AK'} {'JNU'} {'Juneau International' } {'Juneau, AK' } {'Juneau' } {'AK'} {'KTN'} {'Ketchikan International' } {'Ketchikan, AK' } {'Ketchikan' } {'AK'} {'OTZ'} {'Ralph Wien Memorial' } {'Kotzebue, AK' } {'Kotzebue' } {'AK'} {'OME'} {'Nome Airport' } {'Nome, AK' } {'Nome' } {'AK'} {'ABI'} {'Abilene Regional' } {'Abilene, TX' } {'Abilene' } {'TX'} {'ABQ'} {'Albuquerque International Sunport'} {'Albuquerque, NM'} {'Albuquerque'} {'NM'}

Image Analyst
Image Analyst 2022-6-7
Why not just do this:
str = 'Adak Island, AK' ;
commaIndex = strfind(str, ',')
commaIndex = 12
if ~isempty(commaIndex)
output = strtrim(str(commaIndex+1 : end))
else
output = str % No comma in the string so just return the original.
end
output = 'AK'
  4 个评论
Image Analyst
Image Analyst 2022-6-7
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?
Deborah Johnson
Deborah Johnson 2022-6-7
I have attached the file. I'm trying to edit the 3rd column to show just the STATE and remove/erase (extractAfter).
What am I not seeing?

请先登录,再进行评论。


Steven Lord
Steven Lord 2022-6-7
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"
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, ")
justCherry = "Cherry"
fruit
fruit = "Apple, Banana, Cherry"
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 = "Cherry"
fruit
fruit = "Cherry"
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.

Deborah Johnson
Deborah Johnson 2022-6-10
编辑:Deborah Johnson 2022-6-11
I tried it when I got home and really appreciate your time and assistance! Thank You so much!

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

标签

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by