Custom output of the fprintf

4 visualizaciones (últimos 30 días)
Giorgos Papakonstantinou
Giorgos Papakonstantinou el 8 de Feb. de 2013
Is there any way to use fprint with an exponential output field (e) for example:
%15.2e
and not get an output such as 3.00e-05 but 30.00e-06? Thank you
  1 comentario
José-Luis
José-Luis el 8 de Feb. de 2013
No, you'd have to write a custom function.

Iniciar sesión para comentar.

Respuesta aceptada

José-Luis
José-Luis el 8 de Feb. de 2013
Editada: José-Luis el 8 de Feb. de 2013
Here is a small script to do what you asked for:
val = -0.0000001245332; %for example
tempVal = abs(val);
orderOfMag = floor(log10(tempVal));
if val == 0
orderOfMag = 1;
end
offset = 1 - orderOfMag; %Here you can change the number of digits to the left of the point. for three replace one by two, e.g.
valueToPrint = val.*10.^offset;
precision = 6; %number of display digits
expPrecision = 3; %number of digits in the exponent
if orderOfMag > 0
formatStr = ['%0' num2str(expPrecision) 'i'];
else
formatStr = ['-%0' num2str(expPrecision) 'i'];
end
your_string = [num2str(valueToPrint,precision)...
'e' ...
sprintf(formatStr,abs(orderOfMag-1))];
  2 comentarios
Giorgos Papakonstantinou
Giorgos Papakonstantinou el 8 de Feb. de 2013
Thank you for providing me with this script Jose. It is very helpful!
José-Luis
José-Luis el 8 de Feb. de 2013
My pleasure.

Iniciar sesión para comentar.

Más respuestas (1)

Image Analyst
Image Analyst el 8 de Feb. de 2013
That does not seem to be one of the format specifier options built in. See formatSpec section of the fprintf() help documentation. It would be nice if it were though. I've seen other programs where you can specify "Scientific notation" where the exponent can be any number, and "Engineering Notation" where the exponent is a multiple of 3.
  2 comentarios
Giorgos Papakonstantinou
Giorgos Papakonstantinou el 8 de Feb. de 2013
Editada: Giorgos Papakonstantinou el 8 de Feb. de 2013
Thank you. I was searching in the documentation for a long time and I thought I wasn't searching quite enough!
Image Analyst
Image Analyst el 8 de Feb. de 2013
You can send a request for enhancement to support@mathworks.com.

Iniciar sesión para comentar.

Categorías

Más información sobre Low-Level File I/O 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