How to keep only numbers in a cell (.xlsx file)?

10 ビュー (過去 30 日間)
Ivan Mich
Ivan Mich 2021 年 2 月 7 日
編集済み: Ive J 2021 年 2 月 7 日
I have an .xlsx file. In one column there are numbers and text in each cell (e.g. 0.562550 (AHV)).
How could I keep ONLY the number of each cell (e.g. ONLY the 0.562550) ??
I am uploading the file in order to understand.
Thank you in advance

採用された回答

Walter Roberson
Walter Roberson 2021 年 2 月 7 日
c = readcell('test.xlsx');
data = cellfun(@(C) str2double(regexprep(C, '\(.*', '')), c)

その他の回答 (1 件)

Ive J
Ive J 2021 年 2 月 7 日
編集済み: Ive J 2021 年 2 月 7 日
x = readcell('test.xlsx')
nums = cellfun(@(x)sscanf(x, '%f', 1), x)
1.4420
0.3320
0.3560
0.8530
250.8690
8.8190
  2 件のコメント
Walter Roberson
Walter Roberson 2021 年 2 月 7 日
Good point. Though I might suggest adding a size to the sscanf() to prevent it from trying to look for more values, and as a guard against the possibility of having a second value that looked like a number in the cell.
Ive J
Ive J 2021 年 2 月 7 日
編集済み: Ive J 2021 年 2 月 7 日
True, this works only for simple situations like this. Otherwise, regexp would guard against those sloppiness with sscanf and more complex formats. Now I've modified it, tanx!

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

カテゴリ

Help Center および File ExchangeMigrate GUIDE Apps についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by