Extract numeric data from cell

26 ビュー (過去 30 日間)
Baklouti Sana
Baklouti Sana 2021 年 2 月 9 日
コメント済み: Jan 2021 年 2 月 9 日
I need to extract numbers from a cell and put them in a matrix.
My cell is expressed as following:
A={'position: [0.5418702363967896, 0.0005752428551204503, -3.834952076431364e-05, 0.0, 0.0, 0.0]'};
I tried the following example but it does not consider the e-5 .
A1 = regexp(A,'[\d*\.]*\d*','match')
A2 = [A1{:}]
out = str2double(strcat(A2{:}))
I need to get the full number to be able to make some calculations.
Thank you for help.
  3 件のコメント
Baklouti Sana
Baklouti Sana 2021 年 2 月 9 日
It is a cell :)
Jan
Jan 2021 年 2 月 9 日
Then use:
B = A{1}
Now it is a CHAR vector again.

サインインしてコメントする。

採用された回答

Mathieu NOE
Mathieu NOE 2021 年 2 月 9 日
hello
A = {'position: [0.5418702363967896, 0.0005752428551204503, -3.834952076431364e-05, 0.0, 0.0, 0.0]'};
A1 = regexp(char(A),'[-+]?([0-9]*[.])?[0-9]+([eE][-+]?\d+)?','match'); % extract numerical content of string
out = str2double(A1);
  2 件のコメント
Baklouti Sana
Baklouti Sana 2021 年 2 月 9 日
Thank you @Mathieu NOE
This solved my problem!
Stephen23
Stephen23 2021 年 2 月 9 日
編集済み: Stephen23 2021 年 2 月 9 日
More efficient than this answer:
format short G
A = {'position: [0.5418702363967896, 0.0005752428551204503, -3.834952076431364e-05, 0.0, 0.0, 0.0]'};
X = find(A{1}=='[',1);
V = sscanf(A{1}(1+X:end),'%f,')
V = 6×1
0.54187 0.00057524 -3.835e-05 0 0 0

サインインしてコメントする。

その他の回答 (1 件)

Jan
Jan 2021 年 2 月 9 日
A = ['position: [0.5418702363967896, 0.0005752428551204503, ', ...
'-3.834952076431364e-05, 0.0, 0.0, 0.0]'];
D = extractBetween(A, '[', ']');
data = sscanf(D{1}, '%g,')
  1 件のコメント
Baklouti Sana
Baklouti Sana 2021 年 2 月 9 日
@Jan Thank you for your response. However, I am using MATLAB 2016a and the function extractBetween is not supported.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

製品


リリース

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by