MATLAB: Custom output of the fprintf

fprintfsavetext file

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

Best Answer

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))];