フィルターのクリア

find the matching strings in tables

9 ビュー (過去 30 日間)
Birsen
Birsen 2016 年 9 月 23 日
編集済み: Walter Roberson 2016 年 9 月 24 日
Hi,
I have an excel file and I converted to a table. My table has a column called "Locations". The location column contains a long list of string like " Country1-Area1,CityA-52". I would like to rename the sites as follows: Area1,CityA-52. I tried many things to find the first "-" index number in a table. I thought if I find the first index number then I could read the rest of the string. Could not succeed so far. Since it is a table it is more complicated. Any ideas?
Thank you Birsen

採用された回答

Walter Roberson
Walter Roberson 2016 年 9 月 24 日
YourTable.Locations = regexprep( YourTable.Locations, '^[^-]+-', '', 'lineanchors');
  2 件のコメント
Birsen
Birsen 2016 年 9 月 24 日
編集済み: Walter Roberson 2016 年 9 月 24 日
Walter, Thank you for the answer. You are right I had to give a demo. Unfortunately your suggestion did not work. It did not give an error but did not change the result neither.
Here it is what I have;
filename='BkgData.xls'
YourTable = readtable(filename);
Here it is how my table looks like
Country Locations ........
======= ======== ==========
'Argentina' 'Argentina-Area1,CityA-52'
'Belgium' 'Belgium-Area5, 346_6
.................
I used your code
YourTable.Locations = regexprep( YourTable.Locations, '^[^-]+-', '', 'lineanchors');
The 'Argentina-Area1,CityA-52' still stands there as a one piece.
Thanks again
Birsen
Birsen 2016 年 9 月 24 日
Walter,
I was wrong. I made a mistake when i tried for the first time. Your code solved the problem. Thanks again.
Birsen

サインインしてコメントする。

その他の回答 (2 件)

George
George 2016 年 9 月 24 日
You can use regexp.
a = 'Country1-Area1,CityA-52';
expression = '-.+$'; % a dash, one or more characters until the end of the line
[token, ~] = regexp(a, expression, 'match');
token{1}(2:end) % lop off the dashes
ans =
1×11 char array
Area1,CityA-52
  1 件のコメント
Birsen
Birsen 2016 年 9 月 24 日
George, Thank you. Your code works great when the string is a single line, but I have a table. Actually 'a" here is a Table not a single line string. In the table there are thousands of line like "a". I can go around and use your code but there got to be a easier way to split the stings and rearrange columns in Table. Thanks Again
Birsen

サインインしてコメントする。


Image Analyst
Image Analyst 2016 年 9 月 24 日
What about using the strrep() function? If you'd given code for making a demo table, I might have tried it for you. You gotta make it easy for us, or at least that helps.
  1 件のコメント
Birsen
Birsen 2016 年 9 月 24 日
Image Analyst;
I can see I did not provide enough information. Thank you though. strrep() function is to find and replace the matching strings. If I had only a couple of line that would help but here I am trying to split hundreds of them and each time the string (country name) is different. And they are located in a Table column.
Birsen

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by