How do I extract a number from a string?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Pedro Guevara
am 22 Jun. 2019
Bearbeitet: per isakson
am 22 Jun. 2019
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.
2 Kommentare
per isakson
am 22 Jun. 2019
I prefer downloading code/variables, which can be used in testing. Please upload Yp_c and possible more examples in a mat-file.
Akzeptierte Antwort
per isakson
am 22 Jun. 2019
Bearbeitet: per isakson
am 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
0 Kommentare
Weitere Antworten (1)
Fikret Dogru
am 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
0 Kommentare
Siehe auch
Kategorien
Mehr zu Structures finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!