How to Extract Numbers after a particular String from a cell in Matlab
6 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
AARISH NAWAZ
il 6 Apr 2022
Commentato: AARISH NAWAZ
il 25 Mag 2022
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 Commenti
Ive J
il 6 Apr 2022
also:
xlsread is not recommended. Use readtable, readmatrix, or readcell instead. For more information, see Compatibility Considerations.
Risposta accettata
Stephen23
il 6 Apr 2022
Modificato: Stephen23
il 6 Apr 2022
S = load('raw.mat');
raw = S.raw
Method one:
F = @(t)sscanf(t,'Rmin%f_Ymin%f',[1,2]);
C = cellfun(F,raw(2:end,1),'uni',0);
M = vertcat(C{:})
Method two:
tkn = regexp(raw(2:end,1),'^Rmin(\d+\.?\d*)_Ymin(\d+\.?\d*)','tokens','once');
M = str2double(vertcat(tkn{:}))
Method three:
M = sscanf(sprintf('%s@',raw{2:end,1}),'Rmin%f_Ymin%f%*[^@]@',[2,Inf]).'
4 Commenti
Stephen23
il 25 Mag 2022
Modificato: Stephen23
il 25 Mag 2022
tbl = readtable('sum_length_20.xlsx')
Method one:
F = @(t)sscanf(t,'%f_Rmin%f_Ymin%f',[1,Inf]);
C = cellfun(F,tbl.name,'uni',0);
M = vertcat(C{:})
Method two:
tkn = regexp(tbl.name,'^(\d+)\D+(\d+\.?\d*)\D+(\d+\.?\d*)','tokens','once');
M = str2double(vertcat(tkn{:}))
Method three:
M = sscanf(sprintf('%s@',tbl.name{:}),'%f_Rmin%f_Ymin%f%*[^@]@',[3,Inf]).'
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Data Type Conversion in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!