Force arrays to print to screen without multiplier
49 views (last 30 days)
Show older comments
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
0 Comments
Answers (2)
dpb
on 19 Sep 2025 at 17:34
Edited: dpb
on 19 Sep 2025 at 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]
format shorte
vProduct
format longe
vProduct
format bank
vProduct
format shortG
vProduct
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)
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 Comment
Paul
on 19 Sep 2025 at 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.
Star Strider
on 19 Sep 2025 at 17:31
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
format shortG
vProduct
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 Comments
Walter Roberson
on 20 Sep 2025 at 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)
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!