selecting only the numbers from a string variable

Dear all,
I have
A={'BARI 500G' ...
'DUR NOR 18CONmS' ...
'SENSO NORM ST-TUB 75ML '...
'MARL 100S 20CIG'...
'BI BOY WHI RMAL DU NA 15 SK'...
'REGU KR GRA ME FAMIAL 1000 ST GRND'
};
I was wondering if there is a way to choose the numbers from A. In case i have 2 numbers in a string I want to select the second one
For instance,
B=[500 18 78 20 15 1000]

5 件のコメント

antonet
antonet 2012 年 7 月 10 日
sorry for the format. I tried but could not fix it
antonet
antonet 2012 年 7 月 10 日
I know. it is a tough question but only you can help me
Tom
Tom 2012 年 7 月 10 日
Num = regexp(A,'\d') tells you where the digits are, but you'll have to do some work to make those into numbers- for example '1000' will come up as 4 results are there are 4 digits, even though they make one number
antonet
antonet 2012 年 7 月 10 日
I see. So there is not any automatic way. Ok!thanks
Luffy
Luffy 2012 年 7 月 10 日
@Antonet: Do you need those numbers as a vector??

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

 採用された回答

Tom
Tom 2012 年 7 月 10 日

12 投票

B = regexp(A,'\d*','Match');
returns one result for each number found in each string. You will then have to get the last value of each cell and convert it to an array (using str2double)

4 件のコメント

Luffy
Luffy 2012 年 7 月 10 日
Just that you need to convert cell that comes from 4th row before using str2double
antonet
antonet 2012 年 7 月 10 日
can you show me how to do this. Your approach is actually what I want
thanks
Tom
Tom 2012 年 7 月 10 日
B = regexp(A,'\d*','Match');
for ii= 1:length(B)
if ~isempty(B{ii})
Num(ii,1)=str2double(B{ii}(end));
else
Num(ii,1)=NaN;
end
end
Num
antonet
antonet 2012 年 7 月 10 日
Perfect!

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

その他の回答 (2 件)

Luffy
Luffy 2012 年 7 月 10 日

1 投票

C = regexp(A,'[0-9]','match');
disp(C)
So do you need to just display those numbers/return them as a vector

2 件のコメント

antonet
antonet 2012 年 7 月 10 日
編集済み: antonet 2012 年 7 月 10 日
yes, as a vector. thanks
Tom
Tom 2012 年 7 月 10 日
If you want a vector, what do you want for if there is no match for a particular line of the string?

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

GS76
GS76 2020 年 10 月 26 日

0 投票

To whom it may concern:
I have the numbers below in a 1x1 string. How do I seperate them into seperate rows or columns? I want all the numbers between ";" (semi-colons).
Any assistance would be greatly appreciated.
0.0;0.005;0.01;0.015;0.02;0.025;0.03;0.035;0.04;0.045;0.05;0.055;0.06;0.065;0.07;0.075;0.08;0.085;0.09;0.095;0.1;0.105;0.11;0.115;0.12;0.125;0.13;0.135;0.14;0.145;0.15;0.155;0.16;0.165;0.17;0.175;0.18;0.185;0.19;0.195;0.2;0.205;0.21;0.215;0.22;0.225;0.23;0.235;0.24;0.245;0.25;0.255;0.26;0.265;0.27;0.275;0.28;0.285;0.29;0.295;0.3;0.305;0.31;0.315;0.32;0.325;0.33;0.335;0.34;0.345;0.35;0.355;0.36;0.365;0.37;0.375;0.38;0.385;0.39;0.395;0.4;0.405;0.41;0.415;0.42;0.425;0.43;0.435;0.44;0.445;0.45;0.455;0.46;0.465;0.47;0.475;0.48;0.485;0.49;0.495;0.5;0.505;0.51;0.515;0.52;0.525;0.53;0.535;0.54;0.545;0.55

6 件のコメント

Stephen23
Stephen23 2020 年 10 月 26 日
>> str = '0.0;0.005;0.01;0.015;0.02;0.025;0.03;0.035;0.04;0.045;0.05;0.055;0.06;0.065;0.07;0.075;0.08;0.085;0.09;0.095;0.1;0.105;0.11;0.115;0.12;0.125;0.13;0.135;0.14;0.145;0.15;0.155;0.16;0.165;0.17;0.175;0.18;0.185;0.19;0.195;0.2;0.205;0.21;0.215;0.22;0.225;0.23;0.235;0.24;0.245;0.25;0.255;0.26;0.265;0.27;0.275;0.28;0.285;0.29;0.295;0.3;0.305;0.31;0.315;0.32;0.325;0.33;0.335;0.34;0.345;0.35;0.355;0.36;0.365;0.37;0.375;0.38;0.385;0.39;0.395;0.4;0.405;0.41;0.415;0.42;0.425;0.43;0.435;0.44;0.445;0.45;0.455;0.46;0.465;0.47;0.475;0.48;0.485;0.49;0.495;0.5;0.505;0.51;0.515;0.52;0.525;0.53;0.535;0.54;0.545;0.55';
>> vec = sscanf(str,'%f;')
vec =
0.00000
0.00500
0.01000
0.01500
0.02000
0.02500
0.03000
0.03500
0.04000
0.04500
0.05000
0.05500
... more lines here
0.51500
0.52000
0.52500
0.53000
0.53500
0.54000
0.54500
0.55000
Akira Agata
Akira Agata 2020 年 10 月 26 日
Another solution:
str = '0.0;0.005;0.01;0.015;0.02;0.025;0.03;0.035;0.04;0.045;0.05;0.055;0.06;0.065;0.07;0.075;0.08;0.085;0.09;0.095;0.1;0.105;0.11;0.115;0.12;0.125;0.13;0.135;0.14;0.145;0.15;0.155;0.16;0.165;0.17;0.175;0.18;0.185;0.19;0.195;0.2;0.205;0.21;0.215;0.22;0.225;0.23;0.235;0.24;0.245;0.25;0.255;0.26;0.265;0.27;0.275;0.28;0.285;0.29;0.295;0.3;0.305;0.31;0.315;0.32;0.325;0.33;0.335;0.34;0.345;0.35;0.355;0.36;0.365;0.37;0.375;0.38;0.385;0.39;0.395;0.4;0.405;0.41;0.415;0.42;0.425;0.43;0.435;0.44;0.445;0.45;0.455;0.46;0.465;0.47;0.475;0.48;0.485;0.49;0.495;0.5;0.505;0.51;0.515;0.52;0.525;0.53;0.535;0.54;0.545;0.55';
vec = split(str,';');
vec = str2double(vec);
GS76
GS76 2020 年 10 月 26 日
Thank you Akira, much appreciated!
GS76
GS76 2020 年 10 月 26 日
Thank you Stephen, much appreciated!
Rik
Rik 2020 年 10 月 26 日
Adding a clarification: you don't even need to cast the string to a char, either solution can handle strings as well.
GS76
GS76 2020 年 10 月 26 日
Thank you Rik. This is an important point.

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

カテゴリ

ヘルプ センター および File ExchangeCharacters and Strings についてさらに検索

タグ

質問済み:

2012 年 7 月 10 日

コメント済み:

2020 年 10 月 26 日

Community Treasure Hunt

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

Start Hunting!

Translated by