## How do I extract a number from a string?

Pedro Guevara

### Pedro Guevara (view profile)

님이 질문을 제출함. 22 Jun 2019
최근 활동 per isakson

### per isakson (view profile)

님이 편집함. 22 Jun 2019
per isakson

### per isakson (view profile)

님이 답변을 채택함.
Good afternoon. I have the following problem. I have a matrix of type "Cell" in which there is data of type "string", said data is composed of a non-numeric character (a - U-) and a numeric character. I want to extract the numeric character of each of the cells in the matrix. Below I leave some images of how my matrices are made.
I want to extract the numerical values ​​of all the "cells" of the last column.
the matrix is ​​named "Yp_c"
Thank you.

per isakson

22 Jun 2019
Pedro Guevara

### Pedro Guevara (view profile)

22 Jun 2019
I only have this scheduled mars.
Zi= zeros(1,NP);
Zi_c = cell(2,NP);
menospiso =0;
Coe_par=Coe_par; %--->COE DE PART MODAL (R): DEL PASO 6. Del el modo mas Bajo al mas Alto.
Modos_C=Modos_C;
Yp = zeros(NP,NP);
Yp_c= cell(NP+1,NP+1);
NodosT1= sortrows(unique(NodosT(:,2)),'descend');
NodosT= sortrows(NodosT,2,'descend');
%--->Despla Max y Despla translacionales
for f=1:NP
[Pos_GL_Y1] = find( NodosT (:,2) == NodosT1(f,1) );
[Minx_piso,Pminx]= min( NodosT( Pos_GL_Y1,1 ) );
Zi (:,f) = abs( Coe_par(f,1) )* M_Sd(f,1); % desplazamientos
Zi_c (:, f) = [ num2cell( Zi (:,f) ) ; ['Mod',num2str(f)] ] ;
Yp(:, f)= Modos (:,f)*Zi(1,f);
Yp_c (:, f) = [ num2cell(Yp(:, f)) ; ['Mod',num2str(f)] ] ;
Yp_c {f, NP+1} = ['U ',num2str( NodosT ( Pos_GL_Y1 (Pminx,1) , 3 ) )] ;
menospiso=menospiso+1;
end
Thank you

로그인 to comment.

## 답변 수: 2

per isakson

### per isakson (view profile)

님의 답변 22 Jun 2019
per isakson

### per isakson (view profile)

님이 편집함. 22 Jun 2019
채택된 답변

Try this %%-section by section
%%
cac = { 123, 'U 2'; 'Mod3', 789 }; % Sample data
%%
isx = cellfun( @(chr) isa(chr,'char'), cac );
%%
buf = regexp( cac(isx), '\d+', 'match' );
num = cellfun( @str2double, buf, 'uni',false );
%%
cac(isx) = num;
and peek on the result
>> cac
cac =
2×2 cell array
{[123]} {[ 2]}
{[ 3]} {[789]}
>>
After reading "all the "cells" of the last column" more carefully
%%
cac = { 123, 'U 2'; 'Mod3', [] }; % Sample data
%%
isx = cellfun( @(chr) isa(chr,'char'), cac(:,end) );
%%
buf = regexp( cac(isx,end), '\d+', 'match' );
num = cellfun( @str2double, buf, 'uni',false );
%%
cac(isx,end) = num;
and peek on the result
>> cac
cac =
2×2 cell array
{[ 123]} {[ 2]}
{'Mod3'} {0×0 double}
/ R2018b

로그인 to comment.

Fikret Dogru

### Fikret Dogru (view profile)

님의 답변 22 Jun 2019

Hello,
I didn't get your question well but if you want to extract numerical value try cell2mat first or use cell string to get values like Yp_c{:,:} all elements

로그인 to comment.

Translated by