Borrar filtros
Borrar filtros

Split structured text delimated by comma in a table column

42 visualizaciones (últimos 30 días)
mahesh kamad
mahesh kamad el 3 de Mzo. de 2023
Editada: the cyclist el 3 de Mzo. de 2023
I have a table containing a variable for airport which has structured string values like Airport = ["MUMBAI,BOM";"NEW DELHI, DLH"; "JAIPUR,JAI";.......] How to split this variable in two different new variables in same table with one variable having location and another as loaction code.

Respuestas (2)

the cyclist
the cyclist el 3 de Mzo. de 2023
Editada: the cyclist el 3 de Mzo. de 2023
% Create the sample input table
Airport = ["MUMBAI,BOM"; "NEW DELHI, DLH"; "JAIPUR,JAI"];
tbl = table(Airport);
% Create temporary string array, with two columns split at the comma
tmp= split(tbl.Airport,",");
% Extract the location, and put in table
tbl.Location = tmp(:,1);
% Extract the code, and put in table
tbl.Code = tmp(:,2);
tbl
tbl = 3×3 table
Airport Location Code ________________ ___________ ______ "MUMBAI,BOM" "MUMBAI" "BOM" "NEW DELHI, DLH" "NEW DELHI" " DLH" "JAIPUR,JAI" "JAIPUR" "JAI"
You have an extra space for the New Delhi airport. If that is a problem there are functions for removing whitespace.

Morgan
Morgan el 3 de Mzo. de 2023
Try using the function
newStr = split(Airport,',');
You may need to wrap the first argument with string(Airport) to make sure everything is formatted properly for the split function.
This will create an N-by-2 array, the first column will be the location string and the second column is the location code string.
Let me know if you need anything else!
  • Morgan Blankenship, MS, EIT

Categorías

Más información sobre Characters and Strings en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by