Display without new line
147 views (last 30 days)
Show older comments
Dimitrios Anagnostou
on 11 Jul 2023
Commented: Adam Danz
on 11 Jul 2023
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.
0 Comments
Accepted Answer
Adam Danz
on 11 Jul 2023
Edited: Adam Danz
on 11 Jul 2023
To preserve the format of the value, use formattedDisplayText, available since R2021a. This solution wraps the conversion into a function. Note that format strings no not have spaces. shortg instead of short g. If spaces are needed, you can tweek this to support the spaces.
Another benefit of this approach is that the format string is used in both the label and the conversion so there can't be a discrepancy between the label and the format.
x = 1/7;
formats = ["short" "shorte" "shortg" "long" "longe" "longg" "rat" "bank"];
for i = 1:numel(formats)
displayFormattedValue(x,formats(i))
end
function displayFormattedValue(val,formatStr)
% val: scalar value
% formatStr: format string
format(formatStr)
valStr = strtrim(formattedDisplayText(val));
fprintf('Format %s: x = %s\n', formatStr, valStr)
end
2 Comments
Steven Lord
on 11 Jul 2023
Note that running this code will result in the display format remaining 'bank' after it is finished executing. In order to restore the display format to its previous value I'd use the one-input and one-output form of format.
x = 1/7;
formats = ["short" "shorte" "shortg" "long" "longe" "longg" "rat" "bank"];
for i = 1:numel(formats)
displayFormattedValue(x,formats(i))
end
function displayFormattedValue(val,formatStr)
% val: scalar value
% formatStr: format string
% Added an output argument to this line
fmt = format(formatStr);
valStr = strtrim(formattedDisplayText(val));
fprintf('Format %s: x = %s\n', formatStr, valStr)
% Added this line to restore the display format
format(fmt);
% The next two lines just shows that it works
fprintf("Format restored to %s\n", fmt.NumericFormat)
fprintf("Value: %s\n", formattedDisplayText(val))
end
If you want to be extra careful you could create an onCleanup object to restore the format even if the code being executed in displayFormattedValue after the initial format call throws an error before the format can be restored.
Adam Danz
on 11 Jul 2023
x = 1/7;
formats = ["short" "shorte" "shortg" "long" "longe" "longg" "rat" "bank"];
originalFormat = format();
cleanupFormat = onCleanup(@()format(originalFormat));
for i = 1:numel(formats)
displayFormattedValue(x,formats(i))
end
clear cleanupFormat % optional
function displayFormattedValue(val,formatStr)
% val: scalar value
% formatStr: format string
format(formatStr)
valStr = strtrim(formattedDisplayText(val));
fprintf('Format %s: x = %s\n', formatStr, valStr)
end
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)
0 Comments
See Also
Categories
Find more on Logical 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!