Force arrays to print to screen without multiplier

49 vues (au cours des 30 derniers jours)
Matthew Miller
Matthew Miller le 19 Sep 2025 à 16:50
Commenté : dpb le 20 Sep 2025 à 2:42
I work with a number of arrays that I print to the screen for visual inspection. An example is below. I hate the scaling factor (1.0e+03). It is absolutely useless and interferes with visual inspection of data for debugging and anlysis. How do I force Matlab to display all arrays without the scaling factor?
>>vProduct = cross(v1com, v2com, 2)
vProduct =
1.0e+03 *
0.0048 0.0076 3.1440
-0.0049 0.0076 3.9840
-0.0017 0.0076 -0.6840
-0.0049 -0.0048 -4.5480
-0.0017 -0.0048 -0.2940
-0.0017 0.0049 -1.3620

Réponses (2)

dpb
dpb le 19 Sep 2025 à 17:34
Modifié(e) : dpb le 19 Sep 2025 à 20:41
vProduct = 1.0e+03 *[
0.0048 0.0076 3.1440
-0.0049 0.0076 3.9840
-0.0017 0.0076 -0.6840
-0.0049 -0.0048 -4.5480
-0.0017 -0.0048 -0.2940
-0.0017 0.0049 -1.3620]
vProduct = 6×3
1.0e+03 * 0.0048 0.0076 3.1440 -0.0049 0.0076 3.9840 -0.0017 0.0076 -0.6840 -0.0049 -0.0048 -4.5480 -0.0017 -0.0048 -0.2940 -0.0017 0.0049 -1.3620
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
format shorte
vProduct
vProduct = 6×3
1.0e+00 * 4.8000e+00 7.6000e+00 3.1440e+03 -4.9000e+00 7.6000e+00 3.9840e+03 -1.7000e+00 7.6000e+00 -6.8400e+02 -4.9000e+00 -4.8000e+00 -4.5480e+03 -1.7000e+00 -4.8000e+00 -2.9400e+02 -1.7000e+00 4.9000e+00 -1.3620e+03
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
format longe
vProduct
vProduct = 6×3
1.0e+00 * 4.800000000000000e+00 7.600000000000000e+00 3.144000000000000e+03 -4.899999999999999e+00 7.600000000000000e+00 3.984000000000000e+03 -1.700000000000000e+00 7.600000000000000e+00 -6.840000000000000e+02 -4.899999999999999e+00 -4.800000000000000e+00 -4.548000000000000e+03 -1.700000000000000e+00 -4.800000000000000e+00 -2.940000000000000e+02 -1.700000000000000e+00 4.899999999999999e+00 -1.362000000000000e+03
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
format bank
vProduct
vProduct = 6×3
1.0e+00 * 4.80 7.60 3144.00 -4.90 7.60 3984.00 -1.70 7.60 -684.00 -4.90 -4.80 -4548.00 -1.70 -4.80 -294.00 -1.70 4.90 -1362.00
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
format shortG
vProduct
vProduct = 6×3
1.0e+00 * 4.8 7.6 3144 -4.9 7.6 3984 -1.7 7.6 -684 -4.9 -4.8 -4548 -1.7 -4.8 -294 -1.7 4.9 -1362
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
It's apparently a new "feature" that the command window displays the "1.00E+00" multiplier even when it is 1; that didn't use to happen. I also think that is annoying and the prior behavior would be preferred.
If one wants a specific format other than the various options given with the format command, one has to write an output format specification and use fprintf which entails counting columns and building the appropriate number of fields and locating the linefeeds. Here's a place where my complaint since the beginnings of MATLAB that the C-style i/o was chosen over Fortran FORMAT really shows up in the inconvenience.
function write2Darray(x,wide,ndec)
% simple-minded function to display array at given precision, width
[r,c]=size(x);
fmt=sprintf('%%%d.%df',wide,ndec); % the single variable format first '%10.3F', e.g
fmt=[repmat(fmt,1,c) '\n']; % repeat to match array width, add newline
fprintf(fmt,x.') % print w/ format; remember transpose
end
write2Darray(vProduct,10,3)
4.800 7.600 3144.000 -4.900 7.600 3984.000 -1.700 7.600 -684.000 -4.900 -4.800 -4548.000 -1.700 -4.800 -294.000 -1.700 4.900 -1362.000
One can get much more elaborate, of course including passing the format type or the width and/or precision by column as vectors.
Before Mathworks made it essentially impossible to add a Fortran compiler I had a mex file that used the Fortran FORMAT facility to do this kind of thing to be able to avoid the repmat construct since FORMAT is defined such that a field repeat multiplier is possible.
ADDENDUM
The problem with the above approach is, of course, one has to manually call it unless bury it inside other functions in which case the semicolon feature doesn't help to turn it off. It's an area I complained about in the most early days that there's no user ability for any finer control over the command output than the ever-existing set of format choices provided.
It seems like I recall a thread a few years ago of a similar complaint/wish and @Walter Roberson discussed some of what would take to override the builtin functionality and it would be, of course, exceedingly difficult if not impossible.
  1 commentaire
Paul
Paul le 19 Sep 2025 à 22:00
May be feasible to write one's own subclass of double, et., al., for the sole purpose of redefining the disp method, and suffer from any other headaches such an approach might cause.

Connectez-vous pour commenter.


Star Strider
Star Strider le 19 Sep 2025 à 17:31
Experiment with the format function.
Example --
vProduct = 1.0e+03 *[0.0048 0.0076 3.1440
-0.0049 0.0076 3.9840
-0.0017 0.0076 -0.6840
-0.0049 -0.0048 -4.5480
-0.0017 -0.0048 -0.2940
-0.0017 0.0049 -1.3620];
vProduct
vProduct = 6×3
1.0e+03 * 0.0048 0.0076 3.1440 -0.0049 0.0076 3.9840 -0.0017 0.0076 -0.6840 -0.0049 -0.0048 -4.5480 -0.0017 -0.0048 -0.2940 -0.0017 0.0049 -1.3620
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
format shortG
vProduct
vProduct = 6×3
1.0e+00 * 4.8 7.6 3144 -4.9 7.6 3984 -1.7 7.6 -684 -4.9 -4.8 -4548 -1.7 -4.8 -294 -1.7 4.9 -1362
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
There may be more precision in your data than what you posted, so it may appear to be different from what is posted here. It should look similar.
.
  6 commentaires
Walter Roberson
Walter Roberson le 20 Sep 2025 à 0:49
It is not a feature of R2025a / R2025b. I tested on R2025b before posting. It is a feature of MATLAB Answers (and, I suspect, MATLAB Online)
dpb
dpb le 20 Sep 2025 à 2:42
Wonder why make it different...good to not muck up real version.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Produits


Version

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by