Live editor text formatting

35 次查看(过去 30 天)
David Cole
David Cole 2024-10-27,15:43
回答: 埃博拉酱 2024-10-28,14:03
I am using live editor and the fprintf command. I do not want a space between my outputs. I have not included \n but the output looks like double spacing. How can I get the second line to print directly below the first line without an addition line space?
Code
Output

回答(2 个)

Hitesh
Hitesh 2024-10-27,16:37
Hi David Cole,
The "fprintf" function does not inherently add spaces or newlines to your output unless specified. However, if it appears that there is an extra line space, it could be due to how the Live Editor formats the output.
You can get your output directly below the previous output without any additional line spaces by using following method:
  • Concatenate Strings: You can print multiple pieces of text, concatenate them into a single string before using "fprintf".
fprintf(['Transmission Line losses Bus 1-2: 11.4214 MW and 45.6857 MVAR\n', ...
'Transmission Line losses Bus 1-3: 1.8218 MW and 7.2872 MVAR']);
  • Use "disp" with "sprintf": You need to use "disp" with "sprintf" as it controls formatting better in the Live Editor.
disp(sprintf('Transmission Line losses Bus 1-2: 11.4214 MW and 45.6857 MVAR'));
disp(sprintf('Transmission Line losses Bus 1-3: 1.8218 MW and 7.2872 MVAR'));
  • Verify Live Editor Settings: Kindly ensure there are no specific settings in the Live Editor affecting the output format.

埃博拉酱
埃博拉酱 2024-10-28,14:03
When using fprintf in a live script, each line of output is independent. The output of your two calls will be placed in two text boxes, with no blank lines in between, just a certain distance to make it look as if there are blank lines.
If you need them to be in the same text box, you may:
  • Concatenate the strings with line breaks, as shown by @Hitesh
  • NOT use disp with sprintf. This makes no difference as I've tested.
  • Compress your fprintfs into one same line or for loop:

类别

Help CenterFile Exchange 中查找有关 Graphics Object Properties 的更多信息

标签

产品


版本

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by