Extracting numbers from cell using regex

Hi,
I have the following cell:
C = {'2/6';'78/6';'982/47';'11/6'}
I would like to extract all numbers from before / and place them in a new matrix, and all numbers from after / and place them in another matrix.
The result should be something like this:
A=[2;78;982;11] B=[6;6;47;6]
How can i do this? I guess using regex would be the easiest way, but how to write it so it extracts it as explained?
Thank you for your help.

回答 (2 件)

Walter Roberson
Walter Roberson 2017 年 12 月 10 日

1 投票

C = {'2/6';'78/6';'982/47';'11/6'};
One way:
temp = cell2mat(cellfun(@str2double,regexp(C, '(\d+)/(\d+)', 'tokens','once'),'uniform',0));
A = temp(:,1);
B = temp(:,2);
Another way:
temp = cell2mat(regexp(C, '(?<A>\d+)/(?<B>\d+)', 'names'));
A = str2double({temp.A}.');
B = str2double({temp.B}.');
Another way:
temp = cell2mat(cellfun(@str2double,regexp(C,'/','split'),'uniform',0));
A = temp(:,1);
B = temp(:,2);
Another way:
A = str2double(regexp(C,'\d+(?=/)','match','once'));
B = str2double(regexp(C,'(?<=/)\d+','match','once'));
Jos (10584)
Jos (10584) 2017 年 12 月 10 日

0 投票

You can use cellfun to scan every string:
C = {'2/6';'78/6';'982/47';'11/6'}
V = cellfun(@(c) sscanf(c,'%f/%f'), C, 'un', 0) ;
AB = cat(2,V{:})
A = AB(1,:)
B = AB(2,:)

5 件のコメント

kalili
kalili 2017 年 12 月 10 日
I am sorry, I was a bit wrong when writing the question. The data is of type cell, not string: C = {2/6;78/6;982/47;11/6}
Walter Roberson
Walter Roberson 2017 年 12 月 10 日
Jos and I have assumed your C was what you posted before, a cell array of character vectors. Our answers work with the C you had posted earlier. But if your C is a cell array of numeric values:
>> C = {2/6;78/6;982/47;11/6}
C =
4×1 cell array
{[0.333333333333333]}
{[ 13]}
{[ 20.8936170212766]}
{[ 1.83333333333333]}
then it is not possible to recover the original numerators and denominators: you can just get approximations of the reduced fractions:
[A,B] = rat(cell2mat(C))
Jos (10584)
Jos (10584) 2017 年 12 月 10 日
@kalili ... sigh ...
Well, how did you get, for instance, the 2 and the 6 in cell 1 in the first place? Maybe you can skip that step?
kalili
kalili 2017 年 12 月 12 日
Hm, the data was obtained by importing an excel sheet using [~,~,C]=xlsread(filename).
Walter Roberson
Walter Roberson 2017 年 12 月 12 日
What shows up for
class(C{1})
When you use the third output of xlsread(), then anything that looks like a plain number will be converted to numeric but anything else will be left alone. Approximately speaking, it is like
temp = read cells as text
temp_numeric = str2double(temp);
mask = ~isnan(temp_numeric);
temp(mask) = num2cell(temp_numeric(mask))
so '2/6' should have been left alone as text because str2double() if it would be nan. Note that str2num() is not used for this purpose: str2num() would evaluate the text as an expression, getting the 0.333<etc> but that is not going to be used.

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

カテゴリ

ヘルプ センター および File ExchangeData Type Conversion についてさらに検索

タグ

質問済み:

2017 年 12 月 10 日

コメント済み:

2017 年 12 月 12 日

Community Treasure Hunt

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

Start Hunting!

Translated by