get 0 in the end of a regexp function

3 visualizaciones (últimos 30 días)
Adrian Brown
Adrian Brown el 11 de Jun. de 2021
Editada: Stephen23 el 11 de Jun. de 2021
I have a number x= 1.25690e-15 in the first step I extract only the the first part of the x which mean firstPart = 1.25690 using the program bellow, then I used the fuction regexp to split the firstPart varibale to elements. in the result of the program I expect to get : [1 2 5 6 9 0] but Unfortunatly I got only [1 2 5 6 9].
x = 1.25690e-15;
string = sprintf('%.8e', x); % Convert number to a scientific notation string with 8 decimal places of precision
stringParts = strsplit(string,'e'); % Split the string where 'e' is
firstPart = str2double(stringParts(1)); % Get the 1st part of stringParts which is the first part of standard form.
k=str2double(regexp(num2str(firstPart),'\d','match')); % slpit the firstPart variable to elements
I really appreciate any help and suggestion.
  2 comentarios
Sebastiano Marinelli
Sebastiano Marinelli el 11 de Jun. de 2021
of course you can still mantain the 8 decimal, you only have to change a bit the code

Iniciar sesión para comentar.

Respuestas (1)

Stephen23 el 11 de Jun. de 2021
Editada: Stephen23 el 11 de Jun. de 2021
x = 1.2569e-15;
v = regexprep(sprintf('%#.6g',x),{'\.','e.+'},'')-'0'
v = 1×6
1 2 5 6 9 0


Más información sobre Characters and Strings en Help Center y File Exchange.


Community Treasure Hunt

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

Start Hunting!

Translated by