Display without new line
Show older comments
To demonstrate various formats of MATLAB, I use the following script:
x = 1/7;
format short, fprintf(['format short: ']), x % default
format short e, fprintf(['format short e: ']), x
format short g, fprintf(['format short g: ']), x
format long, fprintf(['format long: ']), x
format long e, fprintf(['format long e: ']), x
format long g, fprintf(['format long g: ']), x
format rat, fprintf(['format rat: ']), x % ne pas utiliser avec des nombres irrationnels
format bank, fprintf(['format bank: ']), x % format 'bancaire'
When I run it I get the following output:

How can I display the format description and the corresponding value in the same line?
I apologize if the question is a trivial one.
Thank you very much in advance.
Accepted Answer
More Answers (1)
Fangjun Jiang
on 11 Jul 2023
Edited: Fangjun Jiang
on 11 Jul 2023
use sprintf() and check "doc sprintf" to learn the % Formatting Operator
sprintf('format short: %f',x)
sprintf('format short: %e',x)
If solely for the demonstration of MATLAB Command Window formatting, use
format short, fprintf(['format short: ']);disp(x)
Categories
Find more on Loops and Conditional Statements 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!