Is it possible for matlab to print output numbers like 0.98765432 merely as .987654 with no fronting zero and only six digits (rounded) ?

2 views (last 30 days)
Is it possible for matlab to print output numbers like 0.98765432 merely as .987654 with no fronting zero and only six digits (rounded) ?
  1 Comment
dpb
dpb on 15 May 2022
I don't believe that is within the C Standard i/o specification of a formatting string definition, no...it is helpful in that way of writing prettified fields. Believe you would have to post-process the output to do so to replace the "0." with just "." to get there.
Where do you want/need this to occur?

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 15 May 2022
regexprep(compose("%.6f", YourMatrix), "^0\.", ".")
That would give you a string() array
  4 Comments

Sign in to comment.

More Answers (1)

the cyclist
the cyclist on 15 May 2022
Edited: the cyclist on 15 May 2022
x = 0.98765432;
s = eraseBetween(sprintf("%7.6f",x),1,1)
s = ".987654"
  5 Comments
Frank Lad
Frank Lad on 15 May 2022
Thanks very much to all who have comented on my question. :)
I hope that all who have contributed can see this reply. I am flummoxed by how to enter a general response to the discussion which has started, so I am merely responding to this single comment of "the cyclist". I hope everyone can see it. If not, would the cyclist please forwarde it on to general readership? Thanks. Here goes.
I tried both the strrep(...) method and the eraseBetween(...) method.
Both did the right type of thing,
but my full problem now is that my x is not merely a single number
but a 30 by 5 matrix, all of whose entries are such numbers as 0.98765432
In my code I defined x as a matrix, and
both the strrep and the erasBetween method properly clipped off the first 0 and reduced to the number of decimal places to six of them, thanks.
However, the printout does not come as a matrix, but rather as a long row of all 150 entries
Is there anyway to get the appropriate reduction but to keep the printed response
as a matrix or such reduced numbers ?
The reason for my interest in such a format of output is that I am writing a report about properties of sampling strategies for testing failures among huge batches of production output (tiny capacitors) for which the presumed property of the production process is to put out product quality as Binomial (or Polya) with very tiny probabilities of producing a failure, on theorder of p = 10^{-6} and smaller ... and sampling small quantities of output from batches of 10,000 components. There are quite a few matrices of prior and posterior mass functions that require exhibition (on the order of 30 by 5 each), and all components are within the positive interval (0,1). The presence of the introductory "0." and the decimal values beyond the sixth in "format long" makes the exhibition very cumbersome. Thus my problem.
I have had a similar problem with the exhibition of probability output before in other contexts. Once the matter of printing a full matrix such as I am requesting now is sorted, I think it might be good for Matlab to create a coded command that would make the creation of such output a routine matter.
Thanks to all who have considered this. :) Sincerely, Frank Lad

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!