How to Extract Numbers after a particular String from a cell in Matlab
8 views (last 30 days)
Show older comments
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 Comments
Ive J
on 6 Apr 2022
also:
xlsread is not recommended. Use readtable, readmatrix, or readcell instead. For more information, see Compatibility Considerations.
Accepted Answer
Stephen23
on 6 Apr 2022
Edited: Stephen23
on 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 Comments
Stephen23
on 25 May 2022
Edited: Stephen23
on 25 May 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]).'
More Answers (0)
See Also
Categories
Find more on Spreadsheets in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!