Borrar filtros
Borrar filtros

How to Extract Numbers after a particular String from a cell in Matlab

6 visualizaciones (últimos 30 días)
I have read and assigned the data from Excel file to the variable raw using [num2,txt,raw] = xlsread('Excelfile1.xlsx')
The Variable raw contains 547x2 cells and I want to extract all the numbers after 'Rmin and _Ymin from all the cells in first column of the cell raw. for example 1.1 and 1.1 (see cell raw{2, 1} ).
the cell raw{2, 1} contains the string Rmin1.1_Ymin1.1_Compliance864.1285_It1000 and I just want 1.1 and 1.1 and so on from all the cells of column 1.
thanking you in anticipation
  3 comentarios
Ive J
Ive J el 6 de Abr. de 2022
also:
xlsread is not recommended. Use readtable, readmatrix, or readcell instead. For more information, see Compatibility Considerations.

Iniciar sesión para comentar.

Respuesta aceptada

Stephen23
Stephen23 el 6 de Abr. de 2022
Editada: Stephen23 el 6 de Abr. de 2022
S = load('raw.mat');
raw = S.raw
raw = 547×2 cell array
{'name' } {'length_sum'} {'Rmin1.1_Ymin1.1_Compliance864.1285_It1000'} {[ 709.4780]} {'Rmin1.1_Ymin1.2_Compliance862.1186_It1000'} {[ 709.5536]} {'Rmin1.1_Ymin1.3_Compliance998.8097_It1000'} {[ 712.5063]} {'Rmin1.1_Ymin1.4_Compliance868.5314_It1000'} {[ 707.5380]} {'Rmin1.1_Ymin1.5_Compliance862.6477_It1000'} {[ 707.6292]} {'Rmin1.1_Ymin1.6_Compliance863.5961_It931' } {[ 774.9664]} {'Rmin1.1_Ymin1.7_Compliance857.0956_It1000'} {[ 776.0780]} {'Rmin1.1_Ymin1.8_Compliance862.2392_It1000'} {[ 734.3033]} {'Rmin1.1_Ymin1.9_Compliance854.211_It1000' } {[ 712.0879]} {'Rmin1.1_Ymin1_Compliance859.3505_It1000' } {[ 702.6166]} {'Rmin1.1_Ymin2.1_Compliance890.4593_It1000'} {[ 717.9015]} {'Rmin1.1_Ymin2.2_Compliance872.4193_It1000'} {[ 721.4368]} {'Rmin1.1_Ymin2.3_Compliance868.4197_It1000'} {[ 729.6224]} {'Rmin1.1_Ymin2.4_Compliance855.7838_It1000'} {[ 739.0984]} {'Rmin1.1_Ymin2.5_Compliance865.8766_It1000'} {[ 732.0453]}
Method one:
F = @(t)sscanf(t,'Rmin%f_Ymin%f',[1,2]);
C = cellfun(F,raw(2:end,1),'uni',0);
M = vertcat(C{:})
M = 546×2
1.1000 1.1000 1.1000 1.2000 1.1000 1.3000 1.1000 1.4000 1.1000 1.5000 1.1000 1.6000 1.1000 1.7000 1.1000 1.8000 1.1000 1.9000 1.1000 1.0000
Method two:
tkn = regexp(raw(2:end,1),'^Rmin(\d+\.?\d*)_Ymin(\d+\.?\d*)','tokens','once');
M = str2double(vertcat(tkn{:}))
M = 546×2
1.1000 1.1000 1.1000 1.2000 1.1000 1.3000 1.1000 1.4000 1.1000 1.5000 1.1000 1.6000 1.1000 1.7000 1.1000 1.8000 1.1000 1.9000 1.1000 1.0000
Method three:
M = sscanf(sprintf('%s@',raw{2:end,1}),'Rmin%f_Ymin%f%*[^@]@',[2,Inf]).'
M = 546×2
1.1000 1.1000 1.1000 1.2000 1.1000 1.3000 1.1000 1.4000 1.1000 1.5000 1.1000 1.6000 1.1000 1.7000 1.1000 1.8000 1.1000 1.9000 1.1000 1.0000
  4 comentarios
Stephen23
Stephen23 el 25 de Mayo de 2022
Editada: Stephen23 el 25 de Mayo de 2022
tbl = readtable('sum_length_20.xlsx')
tbl = 546×2 table
name length_sum ________________________________________________ __________ {'100_Rmin1.4_Ymin2.5_Compliance400.1796_It449'} 1443.2 {'101_Rmin1.4_Ymin2.6_Compliance413.2265_It510'} 1467.7 {'102_Rmin1.4_Ymin2.7_Compliance411.9452_It341'} 1468.9 {'103_Rmin1.4_Ymin2.8_Compliance415.3184_It480'} 1464.1 {'104_Rmin1.4_Ymin2.9_Compliance415.2841_It702'} 1452.2 {'105_Rmin1.4_Ymin3_Compliance414.67_It1000' } 1460.4 {'106_Rmin1.5_Ymin1_Compliance398.7844_It333' } 1448.5 {'107_Rmin1.5_Ymin1.1_Compliance403.9337_It189'} 1469.9 {'108_Rmin1.5_Ymin1.2_Compliance398.8366_It311'} 1447.8 {'109_Rmin1.5_Ymin1.3_Compliance398.9644_It336'} 1446.8 {'10_Rmin1_Ymin1.9_Compliance400.2463_It172' } 1494.8 {'110_Rmin1.5_Ymin1.4_Compliance420.9736_It595'} 1497.1 {'111_Rmin1.5_Ymin1.5_Compliance400.0201_It246'} 1456.6 {'112_Rmin1.5_Ymin1.6_Compliance397.5542_It311'} 1463.1 {'113_Rmin1.5_Ymin1.7_Compliance402.336_It270' } 1449.3 {'114_Rmin1.5_Ymin1.8_Compliance401.9567_It303'} 1454.3
Method one:
F = @(t)sscanf(t,'%f_Rmin%f_Ymin%f',[1,Inf]);
C = cellfun(F,tbl.name,'uni',0);
M = vertcat(C{:})
M = 546×3
100.0000 1.4000 2.5000 101.0000 1.4000 2.6000 102.0000 1.4000 2.7000 103.0000 1.4000 2.8000 104.0000 1.4000 2.9000 105.0000 1.4000 3.0000 106.0000 1.5000 1.0000 107.0000 1.5000 1.1000 108.0000 1.5000 1.2000 109.0000 1.5000 1.3000
Method two:
tkn = regexp(tbl.name,'^(\d+)\D+(\d+\.?\d*)\D+(\d+\.?\d*)','tokens','once');
M = str2double(vertcat(tkn{:}))
M = 546×3
100.0000 1.4000 2.5000 101.0000 1.4000 2.6000 102.0000 1.4000 2.7000 103.0000 1.4000 2.8000 104.0000 1.4000 2.9000 105.0000 1.4000 3.0000 106.0000 1.5000 1.0000 107.0000 1.5000 1.1000 108.0000 1.5000 1.2000 109.0000 1.5000 1.3000
Method three:
M = sscanf(sprintf('%s@',tbl.name{:}),'%f_Rmin%f_Ymin%f%*[^@]@',[3,Inf]).'
M = 546×3
100.0000 1.4000 2.5000 101.0000 1.4000 2.6000 102.0000 1.4000 2.7000 103.0000 1.4000 2.8000 104.0000 1.4000 2.9000 105.0000 1.4000 3.0000 106.0000 1.5000 1.0000 107.0000 1.5000 1.1000 108.0000 1.5000 1.2000 109.0000 1.5000 1.3000

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Data Type Conversion en Help Center y File Exchange.

Productos


Versión

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by