Display without new line
147 次查看(过去 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.
0 个评论
采纳的回答
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
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
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
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
2023-7-11
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
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)
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!