MATLAB Answers

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

1 view (last 30 days)
Ivan Mich
Ivan Mich on 7 Feb 2021
Edited: Ive J on 7 Feb 2021
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

Accepted Answer

Walter Roberson
Walter Roberson on 7 Feb 2021
c = readcell('test.xlsx');
data = cellfun(@(C) str2double(regexprep(C, '\(.*', '')), c)

More Answers (1)

Ive J
Ive J on 7 Feb 2021
Edited: Ive J on 7 Feb 2021
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 Comments
Ive J
Ive J on 7 Feb 2021
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!

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!