Display without new line

89 次查看(过去 30 天)
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.

采纳的回答

Adam Danz
Adam Danz 2023-7-11
编辑:Adam Danz 2023-7-11
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
Format short: x = 0.1429 Format shorte: x = 1.4286e-01 Format shortg: x = 0.14286 Format long: x = 0.142857142857143 Format longe: x = 1.428571428571428e-01 Format longg: x = 0.142857142857143 Format rat: x = 1/7 Format bank: x = 0.14
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 个评论
Steven Lord
Steven Lord 2023-7-11
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
Format short: x = 0.1429 Format restored to short Value: 0.1429 Format shorte: x = 1.4286e-01 Format restored to short Value: 0.1429 Format shortg: x = 0.14286 Format restored to short Value: 0.1429 Format long: x = 0.142857142857143 Format restored to short Value: 0.1429 Format longe: x = 1.428571428571428e-01 Format restored to short Value: 0.1429 Format longg: x = 0.142857142857143 Format restored to short Value: 0.1429 Format rat: x = 1/7 Format restored to short Value: 0.1429 Format bank: x = 0.14 Format restored to short Value: 0.1429
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
Adam Danz 2023-7-11
Great thought @Steven Lord.
Here's Steven's onCleanup idea to preserve the original format.
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

请先登录,再进行评论。

更多回答(1 个)

Fangjun Jiang
Fangjun Jiang 2023-7-11
编辑:Fangjun Jiang 2023-7-11
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)

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

标签

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by