Find and Replace (MATLAB)
7 ビュー (過去 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?
その他の回答 (4 件)
Image Analyst 2022 年 6 月 10 日
編集済み: Image Analyst 2022 年 6 月 10 日
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')
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"
The following call creates a new variable named justCherry and leaves the fruit variable with its original contents.
justCherry = extractAfter(fruit, "Banana, ")
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, ")
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.