How to Extract Numbers after a particular String from a cell in Matlab
8 ビュー (過去 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 で Spreadsheets についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!