How to Extract Numbers after a particular String from a cell in Matlab
6 次查看(过去 30 天)
显示 更早的评论
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 个评论
Ive J
2022-4-6
also:
xlsread is not recommended. Use readtable, readmatrix, or readcell instead. For more information, see Compatibility Considerations.
采纳的回答
Stephen23
2022-4-6
编辑:Stephen23
2022-4-6
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 个评论
Stephen23
2022-5-25
编辑:Stephen23
2022-5-25
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]).'
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Type Conversion 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!