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 次查看(过去 30 天)
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 个评论
dpb
dpb 2022-5-15
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?

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2022-5-15
regexprep(compose("%.6f", YourMatrix), "^0\.", ".")
That would give you a string() array
  4 个评论
Frank Lad
Frank Lad 2022-5-16
Hi Walter and the cyclist
Thanks again, but now I think I would like to know if you could figure a way to get the quotation marks off the resulting output.
My output appears as a stringarray in the form of a matrix, but every entry has quotation marks around it. Can thewse be eliminated ?
Thanks a lot for any consideration. Sincerely, Frank Lad (also a cyclist :) )

请先登录,再进行评论。

更多回答(1 个)

the cyclist
the cyclist 2022-5-15
编辑:the cyclist 2022-5-15
x = 0.98765432;
s = eraseBetween(sprintf("%7.6f",x),1,1)
s = ".987654"
  5 个评论
dpb
dpb 2022-5-15
编辑:dpb 2022-5-15
Indeed...although one would presume wouldn't apply it to numbers abs(x(rounded to six places))>=1 since those don't have just a leading 0. that seemed to be the Q?. :)
A one-liner to handle the case doesn't come to me otomh...I'd put this on the caller to not misuse.
Of course, one can also presume the OP would like to have stuff in the command window and elsewhere follow same convention; that, sadly is not possible; the format options are pretty limited. I've often wished could set a format for a table by column -- it's a real pain to have integer variables show up with two decimal points under format bank, but it's even worse for currency values to have varying precision displayed. One way in which spreadsheets have an advantage...
Frank Lad
Frank Lad 2022-5-15
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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Graphics Object Programming 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by